我有一組票,票有這個方案:
{
"_id": {
"$oid": "61bc5ea14bde4bfd53e824c8"
},
"number": {
"$numberLong": "1"
},
"creation_time": {
"$date": "2021-12-17T09:55:45.857Z"
},
"modified_time": null,
"title": "Fix ethernet ports",
"description": null,
"tags": [],
"assigned_to": null,
"created_by": {
"$oid": "61bc5ea14bde4bfd53e824c7"
},
"status": "Opened",
"solution": null,
"priority": "Low",
"confidentiality": "Public",
"changes": [],
"messages": [{
"$oid": "61bc5ea14bde4bfd53e824c6"
}, {
"$oid": "61bc931ca1e7188973dec71b"
}, {
"$oid": "61bf3ef02f689cef7e2aeb1f"
}],
"last_activity": {
"$date": "2021-12-17T09:55:45.857Z"
},
"hidden": false
}
我可以使用 $lookup 查詢
$lookup: {
from: 'messages',
localField: 'messages',
foreignField: '_id',
as: 'messages'
}
它檢索所有訊息,但現在訊息中的欄位是“作者”,它包含用戶檔案的 ID,如何在每條訊息中將作者 ID 替換為相應的用戶檔案?
訊息方案:
{
"_id": {
"$oid": "61bc5ea14bde4bfd53e824c6"
},
"time": {
"$date": "2021-12-17T09:55:45.856Z"
},
"edited_time": null,
"changes": [],
"content": "sdsdsd",
"author": {
"$oid": "61bc5ea14bde4bfd53e824c7"
}
}
用戶方案:
{
"_id": {
"$oid": "61bc5ea14bde4bfd53e824c7"
},
"role": "Employee",
"username": null,
"first_name": "...",
"last_name": "...",
"email": "...",
"token": "...",
"solved_tickets": [],
"reputation": {
"$numberLong": "0"
}
}
我嘗試了很多東西,但總是失敗,我嘗試了好幾天,我覺得我很愚蠢,我一定錯過了一些重要的事情,感覺冒名頂替綜合癥很難
uj5u.com熱心網友回復:
您正在尋找的是嵌套查找,在您的查找中,您可以使用該欄位pipeline將整個聚合添加到其他檔案。
db.collection.aggregate([
{
'$lookup': {
'from': 'messages',
'localField': 'messages',
'foreignField': '_id',
'as': 'messages',
'pipeline': [
{
'$lookup': {
'from': 'user',
'localField': 'author',
'foreignField': '_id',
'as': 'author'
}
}
]
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/413824.html
標籤:
下一篇:c中陣列索引的多個引數
