Pymongo 使用查找加入

Pymongo join using lookup

我有两个合集:

post = {
   "_id" : 102,
   "categories": [
      {'id': ObjectId('6054603f3e1967165a3e70f2'),'name': 'Acting'},
      {'id': ObjectId('605460403e1967165a3e70f6'), 'name': 'Singing'}]
     }
content = {
   "_id" : ObjectId('6054603f3e1967165a3e70f2'),
   "name" : 'Acting'
}

所以我想加入:

out = [{
   "_id" : 102,
   "name" : ["Acting", "Singing"]}]

但在确保两个集合中 Acting 的 id 相同之后。 因此,为此我正在尝试连接两个集合。 这是我尝试过的:

temp = list(posts.aggregate([
    ...:   {"$unwind" : "$categories"},
    ...:   {"$lookup" : {
    ...:                "from" : "content_categories",
    ...:                "localField" : "categories.id",
    ...:                "foreignField" : "_id",
    ...:                "as" : "temp"
    ...:               }
    ...:   },
    ...:   {"$unwind" : "$temp"},
    ...:   {"$group" : {
    ...:                "_id": "$_id", "name":{"$first":"$temp.name"}
    ...:               }
    ...:   }]))

但我只得到单个名称值(我猜是 ID 的第一次出现),而不是名称列表。 并且我已经交叉检查了临时变量中是否存在所有值。然后我应该如何获取这些值作为 name = ["Acting", "Singing"].

这对我有用:

temp = list(posts.aggregate([
          {"$unwind" : "$categories"},
          {"$lookup" : {
                       "from" : "content_categories",
                       "localField" : "categories.id",
                       "foreignField" : "_id",
                       "as" : "temp"
                     }
          },
          {"$unwind" : "$temp"},
          {"$project" : {
                       "_id": 1, "temp.name" : 1
                      }
          }
        ]))