我希望在 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/