[英]Mongodb,find results from array and move to another array
例如:
{
"groups": [
...,
{
"id": 1,
"name": "g1",
"items": [
{
"id": 11,
"name": "item-11"
},
{
"id": 12,
"name": "item-12"
}
]
},
{
"_id": 2,
"name": "g2",
"items": []
}
]
}
使用'更新',groups[0].items 搜索结果,移动到groups[1].items。
预期结果:
{
"groups": [
{
"_id": 2,
"name": "g2",
"items": [
{
"id": 11,
"name": "item-11"
},
{
"id": 12,
"name": "item-12"
}
]
}
]
}
根据对这个问题的回答,在更新中使用另一个字段的值的唯一方法似乎是创建一个脚本。 您尚未指定对groups[0].items
的预期内容,因此我将其设置为空数组:
var requests = [];
var cursor = db.getCollection('collection_name').find({});
cursor.forEach(document => {
requests.push( {
'updateOne': {
'filter': { '_id': document._id },
'update': { '$set': {
'groups.1.items': document.groups[0].items,
'groups.0.items': []
}
}
}
});
if (requests.length === 500) {
//Execute per 500 operations and re-init
db.getCollection("collection_name").bulkWrite(requests);
requests = [];
}
});
if(requests.length > 0) {
db.getCollection("collection_name").bulkWrite(requests);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.