Firebase Firestore - How to query by timestamp date? - javascript

I am trying to use a query that compares the date today as follows:
let today = Date()
cardRef.where('date', '=', today).get().then(function(cardSnapshot) {
cardSnapshot.forEach(function(doc) {
cards[doc.id] = doc.data()
})
})
Using this query, no data is fetched, is the format of the date wrong? The 'date' property is a timestamp from Firestore

Related

How to query for a null fields with graphql?

I'm trying to query for a field that can be null or its _gt, but cannot find a way to make this work.
I've tried this:
query GetBanner {
foo(
sort: "posicao:asc",
where: {
startDate_lt: "2021-10-13T21:13:16.510Z",
_or: [
{ endDate_gt: "2021-10-13T21:13:16.510Z"},
{ endDate: null }
]
}
) {
id
created_at
updated_at
startDate
endDate
}
}
But I'm getting the following error message:
Invalid format, expected a timestamp or an ISO date
How to properly query this?

date created in firebase functions is different from the date created in angular

When a date object is created and set into firestore document using below
Firebase function:
var updateData = {status: 'closed', closedOn: new Date()};
db_fs.collection("bookings").doc(bookingDocId).update(updateData).then(async () => {console.log("Document successfully updated!");});
then it goes into firebase like below
But if I set the date in the request (i.e. angular) instead of setting date inside firebase functions
Angular:
var updateData = {status: 'closed', createdOn: firebase.firestore.Timestamp.fromDate(new Date())};
Firebase function:
const data = req.body;
var updateData = data.updateData;
db_fs.collection("bookings").doc(bookingDocId).update(updateData).then(async () => {console.log("Document successfully updated!");});
then it goes like below
So is there a way in angular/javascript to set the date such that it comes into firebase like shown in the first pic?

How to filter by property of a reference in Firebase's where()

Background
I have two firebase collections, dates and seats, whose (simplified) schema looks like this:
dates: [
{ time: Timestamp }
]
seats: [
{ date: reference to a date }
]
Of course, there is more information on each collection, but the idea here is that multiple seats can link to the same date.
The task at hand is to fetch only seats with dates that lie in the future.
My first instinct was to use 'in':
const futureDates = await firestore.collection(DATES)
.where('time', '>', new Date())
.get()
const seats = await firestore.collection(SEATS)
.where('date', 'in', futureDates.docs.map(date => date.ref))
.get()
But then I could only use 10 dates at once:
Limitations
Note the following limitations for in and array-contains-any:
in and array-contains-any support up to 10 comparison values.
(https://firebase.google.com/docs/firestore/query-data/queries)
Question
Is it possible to do it more elegantly and access the time property of date directly in the search query of seats?
Something along the lines of:
const seats = await firestore.collection(SEATS)
.where('date.time', '>', new Date()) // This always returns 0 seats though
.get()

Query startAt and endAt for a date in firebase

I have the following data structure:
attendance
--- 2020-02-09-PM
--- 2020-02-11-PM
--- 2020-02-16-AM
--- 2020-02-16-PM
--- 2020-02-18-PM
I wanted to get for example the date of 2020-02-16, I would need both PM and AM. So I wanted to query my DB to only get that data.
Here is my attempt:
function getAttendanceCount(orgUid, dates) {
orgUid.forEach(uid => {
dates.forEach(date => {
const getAttendCount = fDB.ref(`organization/${uid}/attendance`)
.orderByChild('attendance')
.startAt(date+'-AM')
.endAt(date+'-PM')
.once('value')
.then(c => console.log(c.val()));
});
})
}
My console.log is null.
Any idea how I could achieve this?
Change this:
const getAttendCount = fDB.ref(`organization/${uid}/attendance`)
.orderByChild('attendance')
Into this:
const getAttendCount = fDB.ref(`organization/${uid}/attendance`)
.orderByKey()
The reason you need to use orderByKey() is because the date are acting as a key and not as a child which would have a value example:
"name" : "peter"

Cannot get collections by date with mongoose

I can not query the collection filtering by a date
The following code is that I have so far
//The following code shows how my schema is:
date: {type: Date, required: true}
//This is a date from a collection in MongoDB:
date: 2019-09-06T16:48:14.000+00:00
//This is how I saved the date in the MongoDB:
"2019/09/09 08:55:15"
//And this is how I perform the query in NodeJS:
let year = req.query.year;
let month = req.query.month;
let day = req.query.day;
let startDate = new Date(year, month, day);
let endDate = new Date(year, month, day);
// find documents in MongoDB
let query = SALES.find({ date: { $gte: startDate, $lte: endDate }});
// execute the query at a later time
query.exec(function (err, item) { // item is a dictionary
if (err) return handleError(err); // throws an error if any
if (item === null || item.length === 0) {
res.json({
status: 'empty',
});
}
else {
res.json({
salesRecord: item
});
}
});
I read that is easy to get, but I am not able to do it. Any help is welcome 🙏
I have not error on the query, simply I get the response as empty.
The expected results is to get the dates from the specified date
Short answer
Date you saved("2019/09/09 08:55:15") is being treated as a string.
Try this:
db.mycollection.find({
"date" : {"$gte": new Date()}
})
or
db.mycollection.find({
"date" : {"$gte": ISODate(new Date())} // current date
});
Description
Mongodb shell provides various methods to return the date.It can be a string or as a Date object:
Date() method which returns the current date as a string.
new Date() constructor which returns a Date object using the ISODate() wrapper.
ISODate() constructor which returns a Date object using the ISODate() wrapper.

Categories

Resources