英文:
Getting 1 week data from firestore database with flutter
问题
I want to pull last 1 week data from firestore database
but the data is not coming, where, what did I do, even I don't know, can you help me?
DateTime oneWeekAgo = DateTime.now().subtract(Duration(days: 7));
Timestamp oneWeekAgoTimestamp = Timestamp.fromDate(oneWeekAgo);
QuerySnapshot snapshot = await FirebaseFirestore.instance
.collection('ogrenciler').where('tc', isEqualTo: widget.tc)
.where('tarih', isGreaterThanOrEqualTo: oneWeekAgoTimestamp)
.orderBy('tarih', descending: true).get();
if (snapshot.docs.isNotEmpty) {
for (QueryDocumentSnapshot documentSnapshot in snapshot.docs) {
print(documentSnapshot.data());
}
} else {
print('Veri yok');
}
英文:
I want to pull last 1 week data from firestore database
but the data is not coming, where, what did I do, even I don't know, can you help me?
DateTime birHaftaOnce = DateTime.now().subtract(Duration(days: 7));
Timestamp birHaftaOnceTimestamp = Timestamp.fromDate(birHaftaOnce);
QuerySnapshot snapshot = await FirebaseFirestore.instance
.collection('ogrenciler').where('tc', isEqualTo: widget.tc)
.where('tarih', isGreaterThanOrEqualTo: birHaftaOnceTimestamp)
.orderBy('tarih', descending: true).get();
if (snapshot.docs.isNotEmpty) {
for (QueryDocumentSnapshot documentSnapshot in snapshot.docs) {
print(documentSnapshot.data());
}
} else {
print('Veri yok');
}
答案1
得分: 1
你的查询是根据文档的“tarih”字段的值进行排序的。但是,根据你分享的屏幕截图,文档的根部没有“tarih”字段。
相反,有一个“gunlukDers”数组字段,其中每个单独的数组元素都有一个“tarih”值。但你想要查询这样的子字段。
解决方法是为你的每个文档添加一个顶层字段,指示日期,然后在该字段上进行查询。
另一种方法是为你现在展示的每个文档创建一个名为“gunlukDers”的子集合,其中存储数组中现在存储的数据,然后在该子集合上进行查询。
英文:
Your query is ordering the documents by the value of their tarih
field. But looking at the screenshot you shared there is no tarih
field in the root of the document.
Instead there is an gunlukDers
array field, where each individual array element has a tarih
value. But you want query for such a subfield.
The solution is to add a top-level field that indicates the date to each of your documents, and then query on that.
An alternative would be to give each of the documents you show now a gunlukDers
subcollection with the data you now store in the array and then query that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论