Get values as array of elements after $lookup

后端 未结 2 1831
既然无缘
既然无缘 2021-01-22 13:55

For MongoDB, when using $lookup to query more than one collection, is it possible to get a values-only list for a field returned in the $lookup?

<
2条回答
  •  借酒劲吻你
    2021-01-22 14:04

    Just use .dot notation with the name field

    db.foo.aggregate([
      { "$lookup": {
        "from": "bar",
        "localField": "name",
        "foreignField": "foo",
        "as": "bars"
      }},
      { "$addFields": { "bars": "$bars.name" }}
    ])
    

    MongoPlayground

提交回复
热议问题