使用新的 firestore,我的结构如下所示:
{
...
"username": "mr_jones",
"date_created": 1508395138572,
"stats": {
"posts": 143,
...
},
...
}
根据文档,我可以order and limit我的查询是为了根据 where
子句过滤数据。我如何根据 stats.posts
对这些数据进行排序?
即
usersRef.orderBy('stats.posts').limit(15);
最佳答案
看起来你的代码中有错别字:
usersRef.orderBy('stats.post').limit(15);
根据您之前的结构,字段名称应该是 stats.posts
:
usersRef.orderBy('stats.posts').limit(15);
我已经仔细检查了这项工作。在控制台中,我创建了一个名为 tester
的集合,它存储了 4 个文档。每个文档都有一个 stats.posts 属性设置为数字 1 到 4。下面的代码打印出 1、2、3。如果我删除限制,它打印出 1、2、3、4。如果我也删除订单它打印出 2、1、3、4。
db.collection('tester').orderBy('stats.posts').limit(3).get().then(function(querySnapshot) {
querySnapshot.forEach(function(documentSnapshot) {
console.log(documentSnapshot.data().stats.posts);
});
});
关于javascript - 子属性(子集合)上的 firestore orderby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46840622/