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

编程入门 行业动态 更新时间:2024-10-28 10:22:45
本文介绍了从集合管道中的集合中减去子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm trying to retrieve a document from a mongodb and remove some objects from an array based on a condition (deleted != null). Here is a simplified example of the item I'm targeting:

{ "_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.像这样:

The successful query will return the same but with one of the image objects removed as it has an ISODate rather than null. Like this:

{ "_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个未缠绕"文档.

I have tried using aggregate to $unwind the images then $match only the relevant images but I now need to recreate the document other wise it returns the remaining 3 'unwound' documents.

这是我到目前为止的位置:

Here is where I am so far:

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); });

是否有一种方法可以从剩余的内容中创建一个文档,还是我会以完全错误的方式来解决这个问题?

Is there a way to create one document from what remains or am I going about this in entirely the wrong way?

谢谢.

推荐答案

正如您提到的那样,您需要将展开,过滤后的文档重新分组为原始形状.您可以使用$group:

As you alluded to, you need to re-group the unwound, filtered docs back into their original shape. You can do this with $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); });

更多推荐

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

本文发布于:2023-11-22 20:22:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618883.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:道中   文档

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!