我有这样的集合:
{
"_id" : ObjectId("59edc30b7d1e1f025f78aaac"),
"_class" : "xyz",
"name" : "Test Segment 7",
"attributes" : [
{
"attributeName" : "Country",
"includedAttributes" : [
{
"$ref" : "attribute",
"$id" : ObjectId("59ddffe8d1f2e34eb9d32f36")
}
]
}
]
}
我想要一个查询将includedAttributes转换为它的等效属性对象。
答案 0 :(得分:1)
以下是适合我的解决方案:
db.collection1.find({}).forEach(function(row){
row.attributes.forEach(function(rAttribute){
if(rAttribute.includedAttributes != undefined){
var embededAttributeArray = [];
rAttribute.includedAttributes.forEach(function(iAttr){
var embeddedAttribute = db.getCollection('attribute').find({"_id":iAttr.$id}).toArray()[0];
embededAttributeArray.push(embeddedAttribute);
});
if(embededAttributeArray != undefined){
rAttribute.includedAttributes.length = 0;
for(var i=0;i<embededAttributeArray.length;i++) {
rAttribute.includedAttributes.push(embededAttributeArray[i])
}
db.collection1.save(row);
}
};
});
});