在 MongoDb 集合中查找空对象属性

Ale*_*ler 5 mongodb

如何在文档或其子文档的属性包含空对象值的 MongoDb 集合中找到所有文档{}?该物业的名称未知。

应退回哪些文件的示例:

{
  data: {
      comment: {}
  }
}
Run Code Online (Sandbox Code Playgroud)

如上所述datacomment作为属性名称是未知的。

Cod*_*ler 7

在聚合管道中迭代对象属性的方法是$objectToArray运算符,它将文档转换为键值对数组。不幸的是,它不会展平嵌入的文档。在实现这种支持之前,我看不到使用纯聚合管道完成任务的方法。

但是,您始终可以使用$where运算符并将逻辑放入 JavaScript 代码中。它应该递归迭代所有文档属性并检查该值是否为空文档。这是一个工作示例:

db.collection.find({"$where" : function () {

    function hasEmptyProperties(doc) {

        for (var property in doc) {
            var value = doc[property];
            if (value !== null && value.constructor === Object &&
                (Object.keys(value).length === 0 || hasEmptyProperties(value))) {
                return true;
            }
        }

        return false;
    }

    return hasEmptyProperties(this);

}});
Run Code Online (Sandbox Code Playgroud)

如果您使用以下数据填充集合:

db.collection.insert({ _id: 1, p: false });
db.collection.insert({ _id: 2, p: [] });
db.collection.insert({ _id: 3, p: null });
db.collection.insert({ _id: 4, p: new Date() });
db.collection.insert({ _id: 5, p: {} });
db.collection.insert({ _id: 6, nestedDocument: { p: "Some Value" } });
db.collection.insert({ _id: 7, nestedDocument: { p1: 1, p2: {} } });
db.collection.insert({ _id: 8, nestedDocument: { deepDocument: { p: 1 } } });
db.collection.insert({ _id: 9, nestedDocument: { deepDocument: { p: {} } } });
Run Code Online (Sandbox Code Playgroud)

查询将正确检测所有具有空属性的文档:

{ "_id" : 5, "p" : {  } }
{ "_id" : 7, "nestedDocument" : { "p1" : 1, "p2" : {  } } }
{ "_id" : 9, "nestedDocument" : { "deepDocument" : { "p" : {  } } } }
Run Code Online (Sandbox Code Playgroud)

仅供参考,这是一个基于$objectToArray它检测空属性的聚合管道,但不在嵌套文档中:

db.collection.aggregate(
[
    { "$project": {
        _id: 1,
        "properties": { "$objectToArray": "$$ROOT" }
    }},

    { "$project": {
        _id: 1,
        propertyIsEmpty: { 
            $map: {
                input: "$properties.v",
                as: "value",
                in: { $eq: ["$$value", {} ] }
            }
        }
    }},

    { "$project": {
        _id: 1,
        anyPropertyIsEmpty: { $anyElementTrue: [ "$propertyIsEmpty" ] }
    }},

    {$match : {"anyPropertyIsEmpty" : true}},

    { "$project": {
        _id: 1,
    }},
]);
Run Code Online (Sandbox Code Playgroud)