如何合并两个mongo集合?

时间:2020-04-09 17:18:25

标签: javascript mongodb

我正在把头撞在墙上……

请参阅更新1(如下)!

我正在将两个集合合并在一起...我看了这个示例(还有SO上的〜几个其他示例)

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#lookup-single-equality

我认为我确实很接近,但是我的预期结果与示例中的预期结果并不相同。

这是“事件”的模式

const EventSchema = new Schema({
    name: {type: String, required: true},
})

这是一些“事件”数据

[
    {
        "_id": "5e8e4fcf781d96df5c1f5358",
        "name": "358 Event"
    },
    {
        "_id": "5e8e55c5a0f5fc1431453b5f",
        "name": "b5f Event"
    }
]

这是“ MyEvent”架构:

const MyEventSchema = new Schema({

    userId: {type: Schema.Types.ObjectId, required: true},
    eventId: {type: Schema.Types.ObjectId, required: true},
})

这是一些“ MyEvent”数据

[
    {
        "_id": "5e8f4ed2ddab5e3d04ff30b3",
        "userId": "5e6c2dddad72870c84f8476b",
        "eventId": "5e8e4fcf781d96df5c1f5358",
    }
]

这是我的代码(代码包装在promise中,因此它返回resolve并拒绝数据)

   var agg = [
       {
         $lookup:
           {
             from: "MyEvent",
             localField: "_id",
             foreignField: "eventId",
             as: "userIds"
           }
       }
    ];

   Event.aggregate(agg)
   .then( events => {
       return resolve(events);
   })
   .catch(err => {
       return reject(null);
   })

这是我的结果,

[
    {
        "_id": "5e8e4fcf781d96df5c1f5358",
        "name": "358 Event",
        "__v": 0,
        "UserIds": []
    },
    {
        "_id": "5e8e55c5a0f5fc1431453b5f",
        "name": "b5f Event",
        "__v": 0,
        "UserIds": []
    }
]

我希望看到事件'358 Event'的UserId填写如下 我想念什么???

[
    {
        "_id": "5e8e4fcf781d96df5c1f5358",
        "name": "358 Event",
        "__v": 0,
        "UserIds": [
          {"userId": "5e6c2dddad72870c84f8476b"}
         ]
    },
    {
        "_id": "5e8e55c5a0f5fc1431453b5f",
        "name": "b5f Event",
        "__v": 0,
        "UserIds": []
    }
]

更新1

我找到了一个mongo游乐场,那里有我的作品,但是在我的代码中不起作用?

https://mongoplayground.net/p/fy-GP_yx5j7

万一链接中断,请按以下步骤进行配置:*选择“ bson多个收藏集”

db={
  "collection": [
    {
      "_id": "5e8e4fcf781d96df5c1f5358",
      "name": "358 Event"
    },
    {
      "_id": "5e8e55c5a0f5fc1431453b5f",
      "name": "b5f Event"
    }
  ],
  "other": [
    {
      "_id": "5e8f4ed2ddab5e3d04ff30b3",
      "userId": "5e6c2dddad72870c84f8476b",
      "eventId": "5e8e4fcf781d96df5c1f5358",

    }
  ]
}

这是查询:

db.collection.aggregate([
  {
    $lookup: {
      from: "other",
      localField: "_id",
      foreignField: "eventId",
      as: "userIds"
    }
  }
])

这是结果:

[
  {
    "_id": "5e8e4fcf781d96df5c1f5358",
    "name": "358 Event",
    "userIds": [
      {
        "_id": "5e8f4ed2ddab5e3d04ff30b3",
        "eventId": "5e8e4fcf781d96df5c1f5358",
        "userId": "5e6c2dddad72870c84f8476b"
      }
    ]
  },
  {
    "_id": "5e8e55c5a0f5fc1431453b5f",
    "name": "b5f Event",
    "userIds": []
  }
]

关于为什么这在我的代码中不起作用...而是在操场上起作用的任何建议?

更新2

我发现了:

Need a workaround for lookup of a string to objectID foreignField

更新3

我现在已经更改了架构以将ObjectId用作ID 仍然不起作用

它们是ObjectIds:

enter image description here

解决方案:

因此,真正的答案是Update 2和Update 3的组合,并在查找中使用正确的集合名称。

Update 2几乎是我的同一个问题...只是使用了不同的表名

更新3是解决此问题的正确方法。

Mohammed Yousry指出集合名称可能是错误的...所以我查看了我的模式,但确实做错了-将名称更改为正确的名称(以及ObjectId类型),就可以了!

1 个答案:

答案 0 :(得分:1)

from的{​​{1}}属性中似乎有一个错字,$lookup也许不是集合名称

MyEvent

在问题所在的蒙哥游乐场,如果您将$ lookup中的“ other”更改为其他任何内容,或在其中输入错误,例如db.collection.aggregate([ { $lookup: { from: "MyEvent", // here is the issue I think, check the collection name and make sure that it matches the one you write here localField: "_id", foreignField: "eventId", as: "userIds" } } ]) 而不是others,将面临同样的问题

因此请确保您填充的单词other中没有错字