我要查询一个具有字典的文档(产品文档),字典定义为文档数组。在C#中,我将其定义为:
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
public Dictionary<int, List<int>> ColorToPhotos { get; set; }
我的目标是地图
from Dictionary<int, List<int>>
to Dictionary<ColorDto, List<PhotoDto>>
因此,我有下一个模式:
产品
[
{
"_id": "1111",
"name": "Awesome T-Shirt"
"colorToPhotos": [
{
k: 1,
v: [3, 4, 5]
},
{
k: 2,
v: [7, 8, 9]
},
...
]
},
...
}
ColorDto
{
"id": integer
"name": string
}
PhotoDto
{
"id": integer
"name": string
}
预期结果(ProductDto)
[
{
"_id": "1111",
"name": "Awesome T-Shirt"
"colorToPhotos": [
{
k: {"_id": 1, "name": "red"}
v: [
{
id: 3,
name: "red-t-shirt-front.jpg",
},
{
id: 4,
name: "red-t-shirt-back.jpg",
},
{
id: 3,
name: "red-t-shirt-child.jpg",
}
]
},
...
]
},
...
}
问题是我的查询包含一个空的“照片”数组。
{
from: 'color',
let: {
'photoIds': '$colorToPhotos.v'
},
pipeline: [
{
$addFields: {
'photoIds': '$$photoIds'
}
},
{
$lookup: {
from: 'photo',
localField: 'photoIds',
foreignField: '_id',
as: 'photos'
}
}
],
as: 'colorToPhotos'
}
结果:
[
{
"_id": "1111",
"name": "Awesome T-Shirt"
"colorToPhotos": [
{
"_id": 1,
"name": "red",
"photoIds": [...],
"photos": []
},
...
]
},
...
}
如何解决查询?