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

2023-12-31

我想在 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)

相反,您应该查询您的收藏 https://firebase.google.com/docs/firestore/query-data/queries?authuser=0#simple_queries:

var ref = db.collection(collection)

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

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

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

This link https://firebase.google.com/docs/firestore/query-data/get-data?authuser=0#get_multiple_documents_from_a_collection应该也有助于理解这个想法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以在 firestore 中查询嵌套文档值吗? 的相关文章

随机推荐