[英]How do I union/merge two Collections by their 'id' using UnderscoreJS
[英]How can I merge two mongo collections?
我正在用头撞墙...
请参阅更新 1(如下)!
我正在将两个 collections 合并在一起...我查看了这个示例(以及这里的〜几个〜其他示例...)
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 中,因此它返回解析并拒绝数据)
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
我找到了这个:
需要一种解决方法来查找 objectID foreignField 的字符串
更新 3
我已更改架构以使用 ObjectId 作为 id 现在仍然无法正常工作
它们是 ObjectId:
解析度:
所以真正的答案是更新 2 和更新 3 的组合,并在查找中使用正确的集合名称。
更新 2 几乎是我的同一个问题......只是使用不同的表名
更新 3 是解决此问题的正确方法。
Mohammed Yousry 指出集合名称可能是错误的......所以我查看了我的架构,但我确实错了 - 将名称更改为正确的名称(以及 ObjectId 类型)并且它有效!
似乎$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
一词中是否没有错字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.