for example:
{
"groups": [
...,
{
"id": 1,
"name": "g1",
"items": [
{
"id": 11,
"name": "item-11"
},
{
"id": 12,
"name": "item-12"
}
]
},
{
"_id": 2,
"name": "g2",
"items": []
}
]
}
Use 'update', groups[0].items search results, move to groups[1].items.
Expected result:
{
"groups": [
{
"_id": 2,
"name": "g2",
"items": [
{
"id": 11,
"name": "item-11"
},
{
"id": 12,
"name": "item-12"
}
]
}
]
}
Based on answers to this question it looks like the only way to use value of another field in an update is to create a script. You haven't specified what is the expected for groups[0].items
so I set it to empty array:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.