![](/img/trans.png)
[英]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.