我无法在mongodb中查询子文档

时间:2017-12-07 09:26:32

标签: node.js mongodb express

我无法在此查询中查询结果, 我想要基于detail.type的结果(比如获取记录where detail.type =" one")并且只获取详细的前10条记录。数字数组

data, type, row

2 个答案:

答案 0 :(得分:0)

MongoDB有助于使用 $ elemMatch 运算符查询数组元素。

根据上述问题中提到的描述作为解决方案,请尝试执行以下MongoDB查询以从MongoDB集合中获取所需数据。

db.collection.find({
    detail: {
        $elemMatch: {
            type: 'One'
        }
    }
}, {
    _id: 1,
    city: 1,
    'detail.$': 1
})

答案 1 :(得分:0)

db.collection.aggregate([

{
 $project:{
  detail:{
   $map:{
    input:{$filter:{input:"$detail",as:"d",cond:{$eq:["$$d.type","One"]}}},
    as:"d",
    in:{
     "type" : "$$d.type",
      "name" : "$$d.name",
      "numbers":{$slice:["$$d.numbers",10]}
    }

   }

  }
 }
}

])