简体   繁体   English

按时间戳月份查询firebase条数据

[英]Query firebase data by timestamp month

So i have my records in firebase cloud firestore like this:所以我在 firebase Cloud firestore 中有这样的记录:

[  
   {  
      "category":"drinks",
      "expensetype":"card",
      "id":"5c673c4d-0a7f-9bb4-75e6-e71c47aa8d1d",
      "name":"JJ",
      "note":"YY",
      "price":57,
      "timestamp":"2017-11-30T22:44:43+05:30"
   },
   {  
      "category":"drinks",
      "expensetype":"card",
      "id":"85731878-eaed-2740-6802-e35e7758270b",
      "name":"VV",
      "note":"TTT",
      "price":40,
      "timestamp":"2017-12-30T22:41:13+05:30"
   }
]

I want to query data with the timestamp and get data that belongs to a particular month or date.我想查询带有时间戳的数据并获取属于特定月份或日期的数据。

For example if I click a particular month (say March) in my calendar, I want only the data of that particular month in my results.例如,如果我在我的日历中单击一个特定的月份(比如三月),我只希望在我的结果中显示该特定月份的数据。 How can i do that?我怎样才能做到这一点?

I have saved the date using firebase.firestore.FieldValue.serverTimestamp()我使用firebase.firestore.FieldValue.serverTimestamp()保存了日期

Thanks in advance for any help.在此先感谢您的帮助。

Edit:编辑:

This is how I form my query这就是我形成查询的方式

var spendsRef = this.db.collection('spend', ref => ref.where("timestamp", ">", "12-12-2017"));

this.items = spendsRef.valueChanges();

But it still returns me all the data in Database但它仍然返回数据库中的所有数据

Answer:回答:

var todayDate = new Date("Tue Jan 02 2018 00:00:00 GMT+0530 (IST)") // Any date in string
var spendsRef = this.db.collection('spend', ref => ref.where("timestamp", ">", todayDate));
this.items = spendsRef.valueChanges();

使用两个相对运算符,可以确定查询返回的文档的精确范围:

ref.where("timestamp", ">=", "2017-11").where("timestamp", "<", "2017-12")

This is the android way 这是android方式

Query query = mFirestore.collection("rootcollection").whereEqualTo("month", 3);

you can further sort the result by timestamp 您可以按时间戳进一步对结果进行排序

Query query = mFirestore.collection("rootcollection")
    .orderBy("timestamp", Query.Direction.DESCENDING)
    .whereEqualTo("month", 3);

I am working with node.js and storing one parameter as new Date(new Date().toUTCString()) .我正在使用 node.js 并将一个参数存储为new Date(new Date().toUTCString())

Now, I fetch the same data, do some processing over it and use the parameter as new Date(<param received in fetch>) in where query to get the same record amongst many records.现在,我获取相同的数据,对其进行一些处理,并在 where 查询中使用参数作为new Date(<param received in fetch>)在许多记录中获取相同的记录。

Thank you @Syed Sehu Mujammil A, your answer worked.谢谢@Syed Sehu Mujammil A,您的回答有效。

I solved a similar problem by:我通过以下方式解决了类似的问题:

 var now = new Date(new Date().toUTCString());
 const db = collection(fireStore, 'jobs_col');
 return await getDocs(query(db, where('expire_date', '>=', now)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM