【问题标题】:Mongo DB query at least one property but preferred more ideally allMongodb 查询至少一个属性,但更理想的是全部
【发布时间】:2016-01-11 06:09:25
【问题描述】:
     "structure":{  
            "country":{  
               "name":"Some Country"
            },
            "city":{  
               "name":"Some City"
            },
            "building":{  
               "name":"Some Building"
            },
            "floor":{  
               "name":""
            }
       }

我有一个像上面这样的对象集合。我需要查询集合,因此它只产生一项。

查询应包括所有四个字段的过滤器:国家、城市、建筑物和楼层。

如果所有四个都匹配,那是理想的情况。

如果集合中没有这样的对象,则查询应返回具有 3 个匹配项和一个为空的项。

如果没有三比二匹配,另外两个应该是空的。

只匹配一个字段也是如此。

其他不匹配的字段必须为空,这一点很重要。不是任何不匹配的值,而是独占 "" 空白空间。

如何为此编写 mongo 查询?

这样的查询对我有用:

    db.structures.find({"some query that I do no know here"}: {
                 "Some Country", 
                 "Some City", 
                 "Some Building", 
                 "Some Floor"
              });

这个查询会从上面返回 json 对象,但它在集合中是对象:

"structure":{  
        "country":{  
           "name":"Some Country"
        },
        "city":{  
           "name":"Some City"
        },
        "building":{  
           "name":"Some Building"
        },
        "floor":{  
           "name":"Some Floor"
        }
   }

它将被返回,因为它具有所有四个匹配项。

【问题讨论】:

  • 如果您可以构建一些测试用例,在其中使用一些示例文档填充测试集合,并为每个测试用例提供带有示例文档的查询的预期输出,那真的会有很长的路要走。
  • @chridam 请检查编辑。
  • 感谢您的编辑,但仍然不确定我是否正确解决了您的问题:因此,如果楼层名称是“Some Floor”作为查询值之一,而其他三个值在数据库中匹配,您希望查询返回楼层名称值为空的文档吗?
  • 我认为只用一个查询是不可能的。有类似 14 种不同的匹配组合(1 种匹配所有四种,3 种匹配 3,6 种匹配 2,4 种匹配 1)
  • 我认为您正在查询中寻找$or 运算符。

标签: json mongodb filtering


【解决方案1】:

我不确定我是否完全理解您的需求... 要搜索与您的 4 个值匹配的数据,请求应如下所示:

db.structures.find({
      "country.name":"Some Country", 
      "city.name":"Some City", 
      "building.name":"Some Building", 
      "floor.name":"Some Floor"
});

【讨论】:

  • 你没看懂问题。
  • 如果您可以编辑查询,以便在匹配 3 个值且第 4 个为“”且匹配 2 个值且第 3 个和第 4 个为“”时加载数据
  • 即使在 SQL 中,我也无法弄清楚如何编写这样的请求? $or 运算符是不够的。
【解决方案2】:

这是我想出的解决方案。

db.structures.findOne({$or:
                  [
                  {$and:[
                    {"country.name": "Some Country"},
                    {"city.name": "Some City"},
                    {"building.name": "Some Building"},
                    {"floor.name": "Some Floor"}]
                   },
                  {$and:[
                    {"country.name": "Some Country"},
                    {"city.name": "Some City"},
                    {"building.name": "Some Building"},
                    {"floor.name": ""}]
                  },
                  {$and:[
                    {"country.name": "Some Country"},
                    {"city.name": "Some City"},
                    {"building.name": ""},
                    {"floor.name": ""}]
                  },
                  {$and:[
                    {"country.name": "Some Country"},
                    {"city.name": ""},
                    {"building.name": ""},
                    {"floor.name": ""}]
                  }]
              });

【讨论】:

    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多