I want every data sent last to be sorted as the last mongo document in my firebase collection. In any case, the last sent data will be stored as the last document in the collection. I used sorting by dates and current time but it is not effective for all dates. (for example date 2.6 puts first instead of 19.6)
This is my function:
Reservations(){
this.user = store.currentUser
this.empty_form=false;
if(this.current_museum!=''&&this.date!=''&&this.ticket_number!=''&&this.user!='')
{
//Date I used for sorting
const current = new Date();
const date = current.getFullYear() '-' (current.getMonth() 1) '-' current.getDate();
const time = current.getHours() ":" current.getMinutes() ":" current.getSeconds();
const dateTime = date ' ' time;
let counter = counter 1;
router.replace({path:'/ticket_reserved', replace: true})
try {
db.collection("reservations")
.doc(dateTime)
.set({
current_museum: this.current_museum,
date: this.date,
ticket_number: this.ticket_number,
user: this.user
});
}
catch(error) {
}
}else{
this.empty_form=true;
}},
CodePudding user response:
I used sorting by dates and current time but it is not effective for all dates. (for example date 2.6 puts first instead of 19.6)
This is a classical problem with date sorting. One common solution is to store the data in the following format: YYYYMMDD
(e.g. 20220602 and 20220619). This way the lexicographic ordering is correct.
You can very well duplicate the date field in your Firestore document: one date
field with the date in the desired date format (most probably of type Timestamp) and another field, e.g. dateSorting
, using the YYYYMMDD format and of type String. You use the second field for building your query.
Note that it works also for date/time format, e.g. YYYYMMDD-HHMMSS
.