J'ai quelques données, produits et ils ont les valeurs des attributs et les id
de cet attribut. Les étiquettes des attributs sont dans la même collection mais avec type: "attribute"
au lieu de `type:" product ". J'ai besoin de rejoindre ceux-ci afin que j'ai les produits, y compris l'étiquette des attributs et leurs valeurs.
Je des données très simples de l'échantillon elle: https://gist.github.com/flowl/632243bca8f2907a672f66920ea0f793
Mon agrégation ressemble à ceci:
db.input.aggregate([
{
$unwind: "$attributes"
},
{
$lookup: {
from: "input",
localField: "attributes.id",
foreignField: "attributeId",
as: "attributeLabels"
}
},
{
$match: { "output": { $ne: [] } }
},
{
$group: {
_id: "$productId",
product: { "$first": "$$CURRENT"}
}
},
{ $group : { _id : "$product._id", data: { $push: "$$ROOT" } } }
]);
Le problème est, je veux reformater la sortie de celle-ci:
{
"_id" : ObjectId("5899925339db9185f13432c4"),
"data" : [
{
"_id" : 111,
"product" : {
"_id" : ObjectId("5899925339db9185f13432c4"),
"type" : "product",
"productId" : 111,
"attributes" : {
"id" : 1,
"value" : "L"
},
"attributeLabels" : [
{
"_id" : ObjectId("5899927539db9185f13432cb"),
"type" : "attribute",
"attributeId" : 1,
"label" : "Size"
}
]
}
}
]
}
Pour cela:
{
"_id" : ObjectId("5899925339db9185f13432c4"),
"type" : "product",
"productId" : 111,
"attributes" : [
{
"_id" : ObjectId("5899927539db9185f13432cb"),
"type" : "attribute",
"attributeId" : 1,
"label" : "Size",
"value" : "L"
}
]
}
Quelle est votre mongo version db? – Veeram
@Veeram db.version() - 3.4.2 – DanFromGermany