deleted != null
我正在尝试从 mongodb 中检索文档并根据条件( )从数组中删除一些对象。这是我要定位的项目的简化示例:
{
"_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"),
}
]
}
成功的查询将返回相同的结果,但删除了其中一个图像对象,因为它具有 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
只使用相关图像,但我现在需要重新创建文档,否则它会返回剩余的 3 个“展开”文档。
这是我到目前为止的位置:
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);
});
有没有办法从剩余的文件中创建一个文档,或者我是否以完全错误的方式处理这个问题?
谢谢。