问题描述
我有一个mongo集合myCollection
,其结构如下:
{ "_id" : ObjectId("xxxxxxx"),"name" : "name1","address" : "address1","list" : [
{ "field1" : true,"field2" : false
},{ "field1" : true,"field2" : false
}]
},...,{ "_id" : ObjectId("zzzzzzzzz"),"name" : "name100","address" : "address100","field2" : true
}]
}
对于集合的每个文档,我想为数组field3
的每个子元素创建一个默认值为false的新字段(list
)。
预期输出:
{ "_id" : ObjectId("xxxxxxx"),"field2" : false,"field3" : false
},"field3" : false
}]
},"field2" : true,"field3" : false
}]
}
解决方法
您可以使用$addFields
添加更多字段
db.collection.aggregate([
{
$addFields: {
"list.fields3": false
}
}
])