简体   繁体   English

如何限制FireBase firestore get()方法返回的记录?

[英]How do I limit records returned by FireBase firestore get() method?

As per the question, I want to retrieve records as per Time basis. 根据问题,我想按时间基础检索记录。

eg 例如

 users:(collection) { uid(document) : { uid(field) : 'someuid', name(field) : 'somename', messages(subcollection) : { '1538484652672'(document) : { text(field) : 'somemessages', time(field) : 1538484652672, } '1538483652672'(document) : { text(field) : 'somemessages', time(field) : 1538483652672, } '1538483652672'(document) : { text(field) : 'somemessages', time(field) : 1538483652672, } . . . . . (possible many thousands entries) } }, uid : { } } 

User is collection inside that name is document, and messages is subcollection. 用户是名称内的集合,名称是文档,消息是子集合。

And in subcollection messages key is like build from 在子集合消息中,键就像从

 console.log(new Date().getTime()); 

I want to retrieve all messages which are posted today and yesterday. 我想检索今天和昨天发布的所有消息。

My Unsuccessful Try : 我失败的尝试:

let backYesterday = new Date(today.getFullYear(), today.getMonth() , today.getDate()-1).getTime();
db.collection('users').where("uid","==",uid).collection('messages')
                     .where("time" , '>' , backYesterday).get()
                     .then(function(querySnapshot) {
                        console.log(querySnapshot);
                        querySnapshot.forEach(function(doc) {
                            console.log(doc.id, " => ", doc.data());
                        });
                    })
                    .catch(function(error) {
                        console.log("Error getting documents: ", error);
                    });

OUTPUT 输出值
User function triggered, starting execution Execution took 3987 ms, user function completed successfully 触发用户功能,开始执行执行耗时3987毫秒,用户功能成功完成

I don't know where I'm going wrong. 我不知道我要去哪里错了。

You're trying to get a subcollection from a query, which isn't possible. 您正在尝试从查询中获取子集合,这是不可能的。 You can only get a subcollection from a specific document. 您只能从特定文档中获取子集合。 This means you first need to execute the query to find the document(s) matching your condition. 这意味着您首先需要执行查询以找到符合您条件的文档。

Since it seems your document's are named after the same UID that you're querying on, you might also be able to get away without a query: 由于似乎您的文档是以您正在查询的相同UID命名的,因此您也可以不用查询就可以逃脱:

var userDoc = db.collection('users').doc(uid)
userDoc.collection('messages')
       .where("time" , '>' , backYesterday).get()

暂无
暂无

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

相关问题 如何使用 Firestore 在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase with Firestore? 如何使用 NextJs API、Firebase Firestore、axios 和 TypeScript 从 Firebase 集合中获取数据? - How do I get data from Firebase collection using NextJs API, Firebase Firestore, axios and TypeScript? AngularJS-如何捕获从GET服务方法返回的JSON,而不管返回的HTTP状态如何 - AngularJS - How do I capture JSON returned from a GET service method regardless of http status returned 使用聚合时如何在运行限制之前获取记录总数 - How do I get the total number of records before I run limit when using Aggregation 如何访问 promise 火库返回的值? - How do I access the value returned by promise firestore? 如何从Firebase Firestore获取带有change.type ==“ added'的实时文档更新 - How do I get Realtime document updates from Firebase Firestore with change.type =="added' 如何按属性值从Firebase / Firestore获取文档 - How do I get documents from Firebase / Firestore ordered by property value 如何获取 Firebase Cloud Firestore 中存在/不存在特定字段的文档? - How do I get documents where a specific field exists/does not exists in Firebase Cloud Firestore? 如何在Firestore中获取1个文档? - How do I get 1 single document in firestore? 我如何在前端的 Firebase Firestore 中计数 - How do I count up in Firebase Firestore in front-end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM