从聚合管道中的集合中减去子文档

Subtract subdocuments from collection in aggregate pipeline

我正在尝试从 mongodb 中检索文档并根据条件 (deleted != null) 从数组中删除一些对象。这是我定位的项目的简化示例:

{
    "_id" : ObjectId("54ec9cac83a214491d2110f4"),
    "name" : "my_images", 
    "images" : [
        {
            "ext" : "jpeg",
            "type" : "image/jpeg",
            "_id" : ObjectId("54f2311026b0cb289ed04188"),
            "deleted" : null,
            "date_added" : ISODate("2015-02-28T21:20:16.961Z"),
        },
        {
            "ext" : "jpeg",
            "type" : "image/jpeg",
            "_id" : ObjectId("54f2314a26b0cb289ed04189"),
            "deleted" : ISODate("2015-02-24T15:38:14.826Z"),
            "date_added" : ISODate("2015-02-28T21:21:14.910Z"),
        },
        {
            "ext" : "jpeg",
            "type" : "image/jpeg",
            "_id" : ObjectId("54f2315526b0cb289ed0418a"),
            "deleted" : null,
            "date_added" : ISODate("2015-02-28T21:21:25.042Z"),
        },
        {
            "ext" : "jpeg",
            "type" : "image/jpeg",
            "_id" : ObjectId("54f2315d26b0cb289ed0418b"),
            "deleted" : null,
            "date_added" : ISODate("2015-02-28T21:21:33.081Z"),
        }
    ]
}

成功的查询将 return 相同,但删除了其中一个图像对象,因为它具有 ISODate 而不是 null。像这样:

{
    "_id" : ObjectId("54ec9cac83a214491d2110f4"),
    "name" : "my_images", 
    "images" : [
        {
            "ext" : "jpeg",
            "type" : "image/jpeg",
            "_id" : ObjectId("54f2311026b0cb289ed04188"),
            "deleted" : null,
            "date_added" : ISODate("2015-02-28T21:20:16.961Z"),
        },
        {
            "ext" : "jpeg",
            "type" : "image/jpeg",
            "_id" : ObjectId("54f2315526b0cb289ed0418a"),
            "deleted" : null,
            "date_added" : ISODate("2015-02-28T21:21:25.042Z"),
        },
        {
            "ext" : "jpeg",
            "type" : "image/jpeg",
            "_id" : ObjectId("54f2315d26b0cb289ed0418b"),
            "deleted" : null,
            "date_added" : ISODate("2015-02-28T21:21:33.081Z"),
        }
    ]
}

我已经尝试使用聚合来 $unwind 图像,然后 $match 只有相关图像,但我现在需要重新创建文档,否则 return 剩下的 3 'unwound' 文档。

这是我目前的位置:

Collection.aggregate([
        { $match:
            { _id: ObjectID(collection_id) }
        },
        { $unwind: "$images" },
        { $match:
            { "images.deleted": null }
        }

        // Next step in the pipeline to
        // reconfigure into one document
        // goes here

    ], function (err, result) {
    if (err) {
        console.log(err);
        return;
    }
    console.log(result);
});

有没有办法从剩下的内容中创建一个文档,或者我是否以完全错误的方式进行此操作?

谢谢。

正如您提到的,您需要将展开的、过滤后的文档重新组合成它们的原始形状。您可以使用 $group:

Collection.aggregate([
        { $match:
            { _id: ObjectID(collection_id) }
        },
        { $unwind: "$images" },
        { $match:
            { "images.deleted": null }
        },

        // Regroup the docs by _id to reassemble the images array
        {$group: {
            _id: '$_id',
            name: {$first: '$name'},
            images: {$push: '$images'}
        }}

    ], function (err, result) {
    if (err) {
        console.log(err);
        return;
    }
    console.log(result);
});