在貓鼬中,我試圖獲取與特定條件匹配的內部記錄。但它總是將兩條記錄都回傳給我,就像它只處理父節點而不是子節點一樣。
$project 聚合函式似乎不像我預期的那樣作業`
[{
"_id": {
"$oid": "636b9958d6ea5d0cc85d20a6"
},
"project_type": 1,
"title": "Pariatur Aut repreh",
"id": 4,
"drawings": [
{
"id": 1,
"assigned_to": 4,
"assigned_name": "Desirae Sandoval",
"assigned_by": 3
},
{
"id": 1,
"assigned_to": 6,
"assigned_name": "Desirae Sandoval",
"assigned_by": 3
}
],
"status": 1,
"created_at": {
"$date": {
"$numberLong": "1667995992724"
}
}
}]
`
I am expecting to get only that drawing which is assigned_to : 6
I have tried aggregate, match, project everything but none of them working, it always fetch both of the drawing, but I am expecting to fetch only one drawing
This what I am expecting
[{
"_id": {
"$oid": "636b9958d6ea5d0cc85d20a6"
},
"project_type": 1,
"title": "Pariatur Aut repreh",
"id": 4,
"drawings": [
{
"id": 1,
"assigned_to": 6,
"assigned_name": "Desirae Sandoval",
"assigned_by": 3
}
],
"status": 1,
"created_at": {
"$date": {
"$numberLong": "1667995992724"
}
}
}]
uj5u.com熱心網友回復:
使用過濾器
db.collection.aggregate([
{
$match: {
"drawings.assigned_to": 6
}
},
{
$project: {
project_type: 1,
title: 1,
id: 1,
status: 1,
created_at: 1,
drawings: {
$filter: {
input: "$drawings",
as: "drawings",
cond: {
$eq: [
"$$drawings.assigned_to",
6
]
}
}
}
}
},
])
編輯:添加一個匹配項以洗掉所有沒有任何檔案的檔案,其中任何一個drawings.assigned_to為 6
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/530456.html
