仅返回mongodb中的嵌套文档

时间:2018-06-14 14:33:39

标签: mongodb mongoose mongodb-query aggregation-framework

基本上我不想将任何键投射到输出中,只是我从查找连接获取的对象...

这是我的代码:

PodMembers.aggregate([
  { $match: { instaid: ObjectId('5a27ed8e1990c12cc0310996'), datetime: { $exists: true } } },
  {
    $lookup: {
      from: "pods",
      localField: "pod_id",
      foreignField: "_id",
      as: "podinfo"
    }
  },
  { $unwind: "$podinfo" },
  {
    $group: {
      _id:"$podinfo"
    }
  },
  { $addFields: { Status: true } }
])

我得到了以下输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用$replaceRoot (aggregation)替换根元素

PodMembers.aggregate([
  { "$match": { "instaid": ObjectId('5a27ed8e1990c12cc0310996'), "datetime": { "$exists": true } } },
  { "$lookup": {
    "from": "pods",
    "localField": "pod_id",
    "foreignField": "_id",
    "as": "podinfo"
  }},
  { "$unwind": "$podinfo" },
  { "$group": {
    "_id":"$podinfo"
  }},
  { "$replaceRoot": { "newRoot": "$_id" } },
  { "$addFields": { Status: true } }
])