javascript - 我可以在 firestore 中查询嵌套文档值吗?

标签 javascript google-cloud-firestore

我希望在 firestore 中搜索以下数据: 收藏->文档->{date{month:10,year:2017}}

var ref = db.collection(collection).doc(document)
ref.where('date.month', '==', 10).get().then(doc=>{
    if (!doc.exists) {
        console.log('No such document!');
    } else {
        console.log('Document data:', doc.data());
    }
}).catch(err => {
    console.log('Error getting document', err);
});

上面的伪代码不起作用。有什么建议么?

最佳答案

看起来您正在查询文档:

var ref = db.collection(collection).doc(document)

相反,您应该查询您的 collection :

var ref = db.collection(collection)

您的查询将选取集合中文档数组中满足“date.month==10”条件的所有文档。

此外,我认为您必须更改解析来自 .get() 的数据的方式,因为它将是一个数组:

.then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            console.log(doc.id, " => ", doc.data());
        });
    })

这个link应该也有助于理解这个想法。

关于javascript - 我可以在 firestore 中查询嵌套文档值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46678037/

相关文章:

firebase - Firebase规则行事很奇怪

javascript - Firestore 执行 get 命令 super 慢

javascript - sinatra javascript 调用

javascript - Bootstrap-tagsinput 中断 .input-group

java - 是否可以在javascript中直接调用Pubsub的订阅拉取,而不是调用java方法来获取数据?

google-cloud-platform - Google Cloud Firestore 与 Google Cloud Spanner 之间的区别?

关于数字的 JavaScript 正则表达式

javascript - document.write 内容替换现有内容而不是显示在其下方

javascript - Firestore 分别按日期和时间查询

firebase - 向 firestore 发送数据很慢