【问题标题】:How can I query where it will only show the documents where it has a field timestamp of today's date?如何查询它只会显示具有今天日期字段时间戳的文档?
【发布时间】:2021-08-02 15:25:47
【问题描述】:

我有这两个“位置”,如果我要为日期添加“位置”,它就不再起作用了。我只想显示所有订单状态为已确认且交货日期为今天的订单。

var start = new Date();
start.setUTCHours(0, 0, 0, 0);
var startOfDay = start.toLocaleDateString();
console.log(startOfDay);

var end = new Date();
end.setHours(23, 59, 59, 999);
var endOfDay = end.toUTCString();
console.log(endOfDay);

try {
  firestore
    .collection("orders")
    .where("orderStatus", "==", "Confirmed")
    .where("deliveryDate", ">=", startOfDay)
    .where("deliveryDate", "<=", endOfDay)
    .onSnapshot((snapshot) => {
      const orders = [];
      snapshot.docs.map((doc) => {
        const data = doc.data();
        orders.push({
          "Order ID": doc.id,
        });
      });
      console.log(orders);
      this.setState({ orders: orders });
      console.log(this.state.orders);
    });
} catch (err) {
  console.log(err);
}

}

这是 firestore 中的时间戳字段

【问题讨论】:

  • 这怎么可能?您怎么能期望任何文档的日期字段与您的客户端生成的日期完全相同?你想达到什么目的?
  • 我只是想从保存在 Firestore 中的日期和客户的日期中获取相同的日期。像同月、同日、同年

标签: javascript reactjs firebase google-cloud-firestore


【解决方案1】:

要获得与给定年、月或日匹配的消息,您必须

  1. yearmonthday 存储为文档中的单独字段

  2. 更改您的查询(例如查询day

    firestore
    .collection("orders")
    .where("orderStatus", "==", "Confirmed")
    .where("day", "==", Math.floor(Date.now()/(1000*3600*24)))
    .get()
    

【讨论】:

    【解决方案2】:

    JavaScript Date object 包含更多信息。根据其文档:

    JavaScript Date 对象以独立于平台的格式表示单个时间点。 Date 对象包含一个 Number,表示自 1970 年 1 月 1 日 UTC 以来的毫秒数。

    因此,当您调用 new Date() 时,它代表了那个确切的时刻,并且您的数据库不太可能包含具有完全相同时间戳的文档。

    这就是为什么您通常希望查询timestamp fields 上的日期范围。因此,您将确定一天的开始和结束的确切日期,然后执行以下操作:

    firestore
        .collection("orders")
        .where("orderStatus", "==", "Confirmed")
        .where("date", ">=", startOfDay)
        .where("date", "<=", endOfDay)
    

    另见:

    【讨论】:

    • 我已经尝试过这样做,但仍然没有得到想要的输出。 var start = new Date();' start.setUTCHours(0, 0, 0, 0); var startOfDay = start.toUTCString(); console.log(startOfDay); var end = new Date(); end.setHours(23, 59, 59, 999); var endOfDay = end.toUTCString(); console.log(endOfDay); try { firestore .collection("orders") .where("orderStatus", "==", "Confirmed") .where("deliveryDate", "&gt;=", startOfDay) .where("deliveryDate", "&lt;=", endOfDay)
    • 要查询日期,您必须过滤时间戳的范围。如果您的代码不起作用,请更新您的问题以显示新代码,并显示您希望查询返回的文档。
    • 我已经编辑了我的问题。谢谢
    • 您不应该将它们转换为字符串。只需将 startend 变量(Date 类型)传递给 Firestore。
    【解决方案3】:
     I have already fixed this and this is the updated code by converting the javascript startofDay and endofday to firebase timestamp.
    
    var start = new Date();
        start.setUTCHours(0, 0, 0, 0);
        var startOfDay = start.toLocaleDateString();
    
        var end = new Date();
        end.setHours(23, 59, 59, 999);
        var endOfDay = end.toUTCString();
      
        var myTimestamp = firebase.firestore.Timestamp.fromDate(
          new Date(startOfDay)
        );
    
        var ending = firebase.firestore.Timestamp.fromDate(new 
         Date(endOfDay));
        console.log("end", ending);
    
        try {
          firestore
            .collection("orders")
            .where("orderStatus", "==", "Confirmed")
            .where("deliveryDate", ">=", myTimestamp)
            .where("deliveryDate", "<=", ending)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多