【问题标题】:Firestore document query [closed]Firestore 文档查询 [关闭]
【发布时间】:2021-02-18 21:13:17
【问题描述】:

所以我使用 Node JS 和 firebase firestore,我想在一个包含文档的集合中创建一个查询,每个文档都有相同的布局,地图(随机 ID)>地图(用户 ID)>{name : “你好”}, 所以我想在整个集合中进行搜索,找到所有具有相同名称值的对象作为搜索文本,这里是数据库的图像,有什么方法可以复合这个查询? 谢谢!!

【问题讨论】:

标签: node.js firebase search google-cloud-firestore


【解决方案1】:

您采用的方法的问题是第二个 randomId,随机性使得无法在单个查询中处理,因此我建议您为此目的更改该结构,如果您将结构更改为以下:

Desks Collection
    uid
    mapContent subCollection
        mapId
        userId
        color
        name

基本上是一样的,只是以子集合的方式组织,可以用这段代码查询:

var arrayOfMaps = [];

var docRef = db.collection("Desks");

docRef.get().then(function(querySnapshot) {
    // In case you want to get only the mapContent document
    querySnapshot.forEach(function(doc) {
        arrayOfMaps.push(db.collection("Desks").doc(doc.id).collection("mapContent")
                           .where("name", "==", "Ikea").get());
    });
    // In case you want to get the whole desks document
    querySnapshot.forEach(function(doc) {
        var snapshot = db.collection("Desks").doc(doc.id).collection("mapContent")
                         .where("name", "==", "Ikea").get();
        if (!snapshot.empty){
            arrayOfMaps.push(doc);
        }
    });
})

【讨论】:

  • 谢谢你让我明白了结构应该更好的方式!
猜你喜欢
  • 2020-12-12
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 2018-07-06
  • 1970-01-01
相关资源
最近更新 更多