Firestore query by date range

前端 未结 9 835
悲&欢浪女
悲&欢浪女 2020-12-02 12:11

I need the help to query long collection with date range. See the below example document. I wanna query startTime field using date range.

相关标签:
9条回答
  • 2020-12-02 12:40

    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;
              })
    
    0 讨论(0)
  • 2020-12-02 12:42

    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);
    
    0 讨论(0)
  • 2020-12-02 12:43
    const event = new Date();
    const expirationDate = admin.firestore.Timestamp.fromDate(event);
    const query = collectionRef.where('startTime', '<=', expirationDate)
    
    0 讨论(0)
提交回复
热议问题