Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb mongoose查询或聚合以删除某些子文档字段_Mongodb_Mongoose - Fatal编程技术网

Mongodb mongoose查询或聚合以删除某些子文档字段

Mongodb mongoose查询或聚合以删除某些子文档字段,mongodb,mongoose,Mongodb,Mongoose,假设我有以下文件 项目 如果我只想返回子文档字段details,这些字段具有is_private===false,那么在mongoose的查询中是否有这样做的方法,或者我是否需要使用聚合 e、 g 如果要使用本机聚合查询执行此操作: db.getCollection('item').aggregate([ { $unwind:"$details" }, { $match:{ "details.is_private":true

假设我有以下文件

项目

如果我只想返回子文档字段
details
,这些字段具有
is_private===false
,那么在mongoose的查询中是否有这样做的方法,或者我是否需要使用聚合

e、 g


如果要使用本机聚合查询执行此操作:

db.getCollection('item').aggregate([
   {
      $unwind:"$details"
   },
   {
      $match:{
         "details.is_private":true
      }
   },
   {
      $group:{
         _id:"$_id",
         details:{
            $push:"$details"
         }
      }
   }
])
输出:

{
    "_id" : ObjectId("5b7d0edb6cfaf771ecf675f0"),
    "details" : [ 
        {
            "title" : "Price",
            "content" : "",
            "is_private" : true,
            "order" : 1.0,
            "type" : "text"
        }
    ]
}

不熟悉猫鼬,但可以为您提供本地猫鼬聚合query@dsharew我也不介意,以防有人不回答。您必须在此处使用
$filter
聚合。。。正如你以前问的。。。即使您使用
$elemMatch
,它也只会返回第一个匹配的文档,并且
是私有的===false
不是所有匹配的文档ones@AnthonyWinzlet是的,我只是好奇猫鼬中是否有这样的查询。聚合也是一个查询。。。因此,您应该询问“是否可以使用find query完成此操作”。。。不是吗?
db.getCollection('item').aggregate([
   {
      $unwind:"$details"
   },
   {
      $match:{
         "details.is_private":true
      }
   },
   {
      $group:{
         _id:"$_id",
         details:{
            $push:"$details"
         }
      }
   }
])
{
    "_id" : ObjectId("5b7d0edb6cfaf771ecf675f0"),
    "details" : [ 
        {
            "title" : "Price",
            "content" : "",
            "is_private" : true,
            "order" : 1.0,
            "type" : "text"
        }
    ]
}