例如,我有这种结构的数据库:
如果我使用此代码:
db.collection("Solar system").doc("Earth").collection("Eurasia").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc);
});
});
我将在控制台中获得一个国家/地区列表:
但是如果我不知道我的数据库中的所有职位并且我只想获得所有职位?
例如,我不知道我的收藏品“地球”中有多少大洲,我想在控制台中打印所有大洲的所有国家的列表。
换句话说,我怎样才能在查询中指定集合和文档的名称?
db.collection("Solar system").doc(?????).collection(?????).get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc);
});
});
答案 0 :(得分:0)
您可以使用根级别集合:
- Planets
- Earth { id: earth }
- Venus { id: venus }
- Mars { id: mars }
- Continents
- Europe { id: europe, planet: earth }
- Asia { id: asia, planet: earth }
- Countries
- Germany { id: germany, continent: europe, planet: earth }
- France { id: france, continent: europe, planet: earth }
根据你的问题“例如,我不知道我的收藏中有多少大陆”地球“,我想在控制台中打印所有大洲的所有国家的列表。”,通过这种结构你可以这样做方式:
db.collection("Countries").where("planet", "==","earth")
所有“欧洲”大陆的国家:
db.collection("Countries").where("continent", "==", "europe")