【问题标题】:Return only nested document in mongodb [duplicate]仅返回 mongodb 中的嵌套文档 [重复]
【发布时间】:2018-11-24 09:04:51
【问题描述】:

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

这是我的代码:

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

我得到以下输出:

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework


    【解决方案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 } }
    ])
    

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 2017-10-28
      • 2018-03-09
      • 2018-02-18
      • 2018-08-28
      • 2020-03-14
      • 1970-01-01
      • 2021-11-12
      • 2019-08-06
      相关资源
      最近更新 更多