简体   繁体   English

如何合并两个 mongo collections?

[英]How can I merge two mongo collections?

I am banging my head against the wall on this...我正在用头撞墙...

SEE UPDATE 1 (below) !请参阅更新 1(如下)!

I am merging two collections together... I looked at this example ( and ~several~ other examples here on SO... )我正在将两个 collections 合并在一起...我查看了这个示例(以及这里的〜几个〜其他示例...)

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

I think I am really close, but my expected results are not the same as what I would expect out of the example.我想我真的很接近,但我的预期结果与我对示例的预期不同。

Here is the schema for 'Event'这是“事件”的架构

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

Here is some 'Event' data这是一些“事件”数据

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

Here is 'MyEvent' schema:这是“MyEvent”架构:

const MyEventSchema = new Schema({

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

Here is some 'MyEvent' data这是一些“MyEvent”数据

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

Here is my code ( the code is wrapped in a promise so it returns resolve and reject with data )这是我的代码(代码包含在 promise 中,因此它返回解析并拒绝数据)

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

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

Here are my results,这是我的结果,

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

I expect to see UserIds filled in for event '358 Event', like this What am I missing???我希望看到为事件“358 事件”填写的用户 ID,就像这样我错过了什么???

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

UPDATE 1更新 1

I found a mongo playground and what I have works there, but it doesn't work in my code??我找到了一个 mongo 游乐场,我在那里工作,但它在我的代码中不起作用?

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

In case the link breaks, here is configuration: * select 'bson multiple collections'如果链接中断,这里是配置:* select 'bson multiple collections'

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

    }
  ]
}

Here is Query:这是查询:

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

Here is the result:结果如下:

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

any suggestions as to why this doesn't work in my code... but works in the playground?关于为什么这在我的代码中不起作用的任何建议......但在操场上起作用?

UPDATE 2更新 2

I found this:我找到了这个:

Need a workaround for lookup of a string to objectID foreignField 需要一种解决方法来查找 objectID foreignField 的字符串

UPDATE 3更新 3

I have changed the schema to use ObjectId for ids now still doesn't work我已更改架构以使用 ObjectId 作为 id 现在仍然无法正常工作

And they are ObjectIds:它们是 ObjectId:

在此处输入图像描述

RESOLUTION:解析度:

So the real answer was a combination of Update 2 and Update 3 and using the right collection name in the lookup.所以真正的答案是更新 2 和更新 3 的组合,并在查找中使用正确的集合名称。

Update 2 is pretty much my very same question... just using different table names更新 2 几乎是我的同一个问题......只是使用不同的表名

Update 3 is the correct way to solve this issue.更新 3 是解决此问题的正确方法。

Mohammed Yousry pointed out the collection name might be wrong... so I looked at my schema and I did have it wrong - changed the name to the right name (along with ObjectId types) and it worked ! Mohammed Yousry 指出集合名称可能是错误的......所以我查看了我的架构,但我确实错了 - 将名称更改为正确的名称(以及 ObjectId 类型)并且它有效!

It seems there's a typo in from property in $lookup , MyEvent maybe not the collection name似乎$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"
    }
  }
])

in mongo playground you attached in the question, if you change the 'other' in the $lookup to anything else, or make a typo in it.. like others instead of other , you will face the same issue在问题中附加的 mongo 操场上,如果您将 $lookup 中的“其他”更改为其他任何内容,或者在其中打错字.. 像others而不是other人一样,您将面临同样的问题

so check that there is no typo in the word MyEvent that you populate from因此请检查您从中填充的MyEvent一词中是否没有错字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用UnderscoreJS通过'id'联合/合并两个集合 - How do I union/merge two Collections by their 'id' using UnderscoreJS 如何在mongodb中合并两个collections,聚合mongodb - How to merge two collections in mongodb, aggregate mongodb 如何从 Firestore 两个 collections 获取数据并将所有数据合并在一起 - how can get data from firestore two collections and merge all data together 如何合并两个 onChange 事件? - How can I merge two onChange events? 如何使用 Lodash 基于一个键合并两个集合? - How to use Lodash to merge two collections based on a key? 如何根据文档中的参考ID合并并观察Firestore中的两个集合? - How to merge and observe two collections in Firestore based on reference ID in documents? 如何合并两个对象数组并连接值? - How can I merge two array of objects and concatenate values? 如何在JavaScript中按索引合并两个对象数组? - How can I merge two object arrays by index in JavaScript? 如何基于值合并两个不同长度的对象 - How can I merge two objects of different lengths based on the values 如何在JavaScript中合并两个对象数组 - How can I merge two arrays of objects in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM