我建議架構更改,雖然這樣:
[
{
"name" : [
{
"language" : "es",
"description" : "Producto 1"
},
{
"language" : "en",
"description" : "Product 1"
}
],
"reference" : "PR1",
"price" : "20",
"description" : [
{
"language" : "es",
"description" : "Descripción 1"
},
{
"language" : "en",
"description" : "Description 1"
}
]
}
]
原因,那麼你可以實際上你的情況語言組。 如果這是可能的,那麼你可以做到以下幾點:因爲你沒有提供完整的JSON
db.products.aggregate([
{$unwind: "$someName"},
{$unwind: "$someName.name"},
{$unwind: "$someName.description"},
{
$group: {
_id: {namelang: "$someName.name.language", namedesc: "$someName.description.language"},
name: {$first: "$someName.name.description"},
reference: {$first: "$someName.reference"},
price: {$first: "$someName.price"},
description: {$first: "$someName.description.description"}
}
},
{
$match: {
"_id.namelang": "en",
"_id.namedesc": "en"
}
}
])
FYI我創建一個隨機的。這裏是完整的模式:
{
"_id" : ObjectId("59ea07a600f5db2660dbf162"),
"someName" : [
{
"name" : [
{
"language" : "es",
"description" : "Producto 1"
},
{
"language" : "en",
"description" : "Product 1"
}
],
"reference" : "PR1",
"price" : "20",
"description" : [
{
"language" : "es",
"description" : "Descripción 1"
},
{
"language" : "en",
"description" : "Description 1"
}
]
}
]
}
這是不可能與find。你必須使用聚合或替代。你的完整json是怎樣的? –
嗨,亞歷克斯,感謝您的快速回答。你能否提供一個使用聚合的例子來得到這個迴應?謝了哥們。 – Jorge