【问题标题】:How to find documents that contains a field that contains any sub-fields如何查找包含包含任何子字段的字段的文档
【发布时间】:2025-11-25 01:30:01
【问题描述】:

我的文档包含 attributes 字段。像这样的:

{
    "_id" : "somestring",
    ...,
    "attributes" : {
        "SomeKey" : {
            "code" : "SomeCode",
            "name" : "SomeName",
        }
    }
}

如何查找所有具有 attributes 字段且包含 1 个或多个子字段的文档?

上面的文档会匹配,但下面的文档不匹配。

{
    "_id" : "somestring",
    ...,
    "attributes" : {}
}

我知道如何查询具有多个项目的数组,并查询具有具有某些特定子字段的字段的文档,但我正在寻找具有具有任何子字段的字段的文档字段。

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    除了使用$where 来运行.js 代码通过查询,您可以使用如下尝试:

    db.collection.aggregate([
      {
        $match: {
          attributes: {
            $ne: {}
          }
        }
      }
    ])
    
    /** Or with `.find()` */
    
    db.collection.find({ attributes: { $ne: {} } });
    

    测试: MongoDB-Playground

    以防万一您根本没有 attributes 或者它存在但不是对象:

    db.collection.aggregate([
      {
        $match: {
          $expr: {
            $and: [
              { $eq: [ { $type: "$attributes" } , "object" ] },
              { $ne: ["$attributes" , {} ] }
            ]
          }
        }
      }
    ])
    
    /** Or with `.find()` */
    db.collection.find({
      $expr: {
        $and: [
          { $eq: [{ $type: "$attributes" }, "object"] },
          { $ne: ["$attributes", {}] },
        ],
      },
    });
    

    测试: MongoDB-Playground

    【讨论】:

    • 我没想过要使用聚合。我比我的答案更喜欢这个。谢谢。
    • @Kit:是的,你也可以使用'.find()' :-)
    【解决方案2】:

    我发现了一个使用$where 的机制,但是因为它是Javascript,所以运行速度很慢。其他内置的 operators 似乎都不适合。

    db.getCollection('COL')
      .find({
          $where: function() {
            for (field in this["attributes"])
              return true;
            return false;
          }
      })
    

    【讨论】: