I need the help to query long collection with date range. See the below example document. I wanna query startTime field using date range.
The solution is to use Date.now (), stop using timestamp service from firebase, you need to work with the numerical value of the time in milliseconds like for example: 1514271367000, instead if firestore uses 26/12/2017 1:56:07 GMT- 0500 (-05) will not work. An example of a query is:
this.fsService.afs.collection('chats/4bY1ZpOr1TPq8bFQ3bjS/finance/123+finance/12345'
, ref => ref.orderBy('hour').startAt(1514184967000).endAt(1514271367000))
.valueChanges().subscribe(data =>{
this.mensajes = data;
})
For everyone recently using firbase firestore. There's a differency depending on your settings of your firebase implentation (depending on the firebase version).
Before, firestore was saving Timestamp
as a Date
, however as described here in the docs the will be replaced soon by a Timestamp
object. See the Timestamp docs here.
You can force your implementation already by adding a setting in your code to force firebase to use Timestamp objects instead of Date like this example:
var firebaseApp = firebase.initializeApp({
apiKey: [APIKEY],
authDomain: [FIREBASEAPPDOMAIN],
projectId: [PROJECTID]
});
var firestore = firebase.firestore();
var settings = { timestampsInSnapshots: true }; // force Timestamp instead of Date
firestore.settings(settings);
const event = new Date();
const expirationDate = admin.firestore.Timestamp.fromDate(event);
const query = collectionRef.where('startTime', '<=', expirationDate)