[英]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.