【问题标题】:How can I merge two mongo collections?如何合并两个 mongo 集合?
【发布时间】:2020-07-22 09:33:24
【问题描述】:

我正用头撞墙……

查看更新 1(如下)!

我正在将两个集合合并在一起...我查看了这个示例(以及这里的〜几个〜其他示例...)

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

这是一些“我的事件”数据

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

这是我的代码(代码包含在一个承诺中,因此它返回解析并拒绝数据)

   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 事件”填写的用户 ID,像这样 我错过了什么???

[
    {
        "_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

如果链接断开,这里是配置: * select 'bson multiple collections'

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 :

解决方案:

所以真正的答案是更新 2 和更新 3 的组合,并在查找中使用正确的集合名称。

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

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

Mohammed Yousry 指出集合名称可能是错误的......所以我查看了我的架构,但我确实错了 - 将名称更改为正确的名称(以及 ObjectId 类型)并且它有效!

【问题讨论】:

    标签: javascript mongodb


    【解决方案1】:

    $lookup 中的from 属性似乎有错字,MyEvent 可能不是集合名称

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

    在您在问题中附加的 mongo 操场上,如果您将 $lookup 中的“其他”更改为其他任何内容,或者在其中打错字......比如others 而不是other,您将面临同样的问题问题

    因此请检查您填充的单词 MyEvent 中是否有拼写错误

    【讨论】:

    • 好吧...如果您创建架构并将其命名为“MyEvent”,则不会有问题。
    • 我并不是说'MyEvent' Schema 名称有问题,我的意思是这个集合名称可能有错别字,可能是'myEvent' 而不是'MyEvent',我意思是在 $lookup 中而不是在命名中,你当然可以根据需要命名模式,
    • 不是这样。它与字符串和对象 id 相关......因为 mongodb 游乐场完美运行
    • 嗯......有点......我的收藏甚至没有命名为'MyEvent'或'MyEvents'它被命名为'it_repo'并且我使用的是'itrepo'......所以你把我在正确的轨道上
    • stackoverflow.com/users/12371475/mohammed-yousry - 这是个好问题吗?它是否概述了您想出答案所需的所有必要信息?如果是这样,你能投票吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 2011-11-24
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多