按日期范围查询Firestore

Nim*_*hty 43 firebase google-cloud-firestore

我需要帮助来查询具有日期范围的长集合.请参阅以下示例文档.我想使用日期范围查询startTime字段.

在此输入图像描述

在此输入图像描述

Cap*_*apy 75

由于我将'dueDate'字段存储为云防火墙上的时间戳(而不是字符串数字),我这样做是为了获取2017年到期日的发票文件:

        let start = new Date('2017-01-01');
        let end = new Date('2018-01-01');

        this.afs.collection('invoices', ref => ref
            .where('dueDate', '>', start)
            .where('dueDate', '<', end)
        );
Run Code Online (Sandbox Code Playgroud)

注意: dueDate字段存储在带有Date()对象的firebase中.例如:this.doc.dueDate = new Date('2017-12-25')

  • 朋友,我试过这个,但它什么也没返回 (3认同)
  • 哇,真棒-一定要添加到Firestore查询文档中。我有种感觉,我们将看到对Firebase-firestore控制台的许多更新。“ Marce de tiempo”-今天最有价值的教训,格拉西亚斯;) (2认同)

小智 23

    var startfulldate = admin.firestore.Timestamp.fromDate(new Date(1556062581000));
    db.collection('mycollection')
      .where('start_time', '<=', startfulldate)
      .get()
      .then(snapshot => {              
            var jsonvalue: any[] = [];
            snapshot.forEach(docs => {
              jsonvalue.push(docs.data())
               })
                 res.send(jsonvalue);
                 return;
                }).catch( error => {
                    res.status(500).send(error)
                });
Run Code Online (Sandbox Code Playgroud)

  • 伟大的 !你救了我的命,伙计。我刚刚用 firebase.firestore 替换了 admin.firestore 并且它起作用了。 (2认同)

Dau*_*eDK 17

您可以将datetime对象存储为Unix时间(自1970年1月1日起的秒数).然后你可以简单地使用where选择如下:

collectionRef.where("startTime", ">=", "1506816000").where("startTime", "<=", "1507593600")

顺便说一句 - 要在你的应用程序中从datetime转换为Unix时间,你可以使用优秀的库时刻(如果你用js或节点构建一些东西).


小智 16

const event = new Date();
const expirationDate = admin.firestore.Timestamp.fromDate(event);
const query = collectionRef.where('startTime', '<=', expirationDate)
Run Code Online (Sandbox Code Playgroud)


小智 13

startTime存储为 时,Timestamp您可以更准确地执行此查询范围(这对于长日期范围或相同日期范围的条件都有好处)。

const start = new Date('2021-01-01T00:00:00.000z');
const end = new Date('2021-03-01T23:59:59.000z');

db.collection('Data').where('startTime', '>=', start).where('startTime', '<=', end).get().then(data => {
   //pass your 'data' here
});
Run Code Online (Sandbox Code Playgroud)

我在 Node.js 应用程序中使用了它。希望这有用。


Thd*_*hdK 8

对于最近使用 Firebase Firestore 的每个人,根据您的 Firebase 实现设置(取决于 Firebase 版本)会有所不同。

之前,Firestore 保存TimestampDate,但是如文档中所述,它将很快被Timestamp对象替换。请参阅此处时间戳文档

您可以通过在代码中添加设置强制 Firebase 使用 Timestamp 对象而不是 Date来强制您的实现如下例所示:

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);
Run Code Online (Sandbox Code Playgroud)


小智 5

解决方案是使用 Date.now()。停止使用 Firebase 的时间戳服务,您需要使用以毫秒为单位的时间数值,例如:1514271367000,如果 Firestore 使用 26/12/2017 1:56:07 GMT- 0500 (-05) 将不起作用。查询的一个示例是:

this.fsService.afs.collection('chats/4bY1ZpOr1TPq8bFQ3bjS/finance/123+finance/12345'
          , ref => ref.orderBy('hour').startAt(1514184967000).endAt(1514271367000))
          .valueChanges().subscribe(data =>{
            this.mensajes = data;
          })
Run Code Online (Sandbox Code Playgroud)

  • Firestore 将 Date.now() 保存为数字数据可以吗?我觉得在某些情况下约会会更好,因为这样也更容易理解。 (2认同)