How can an existing document be updated using it's own values?
A collection of Album exists which contains photos, the photo webpath needs modifying to prepend the existing value.
The following is based on the answer https://stackoverflow.com/a/3792958/1283381
The code below fails to update the webPath value.
db.getCollection('Album').find().snapshot().forEach(
function (e) {
for(var i=0; i<e.photos.length; i++) {
e.photos[i].webPath = '/uploads'+e.photos[i].webPath;
}
db.events.save(e);
}
)
Output is:
Updated 1 existing record(s) in 2ms
Edit 1
Sample Album document:
{
"_id" : ObjectId("5923e3eebe37843815248521"),
"created" : ISODate("2017-05-23T07:25:34.000Z"),
"photos" : [
{
"id" : 474,
"webPath" : "/dir/to.jpg"
},
{
"id" : 475,
"webPath" : "/dir/to.jpg"
}
]
}