简体   繁体   中英

rename field in mongoDB and set newValue at the same time

I want to rename a field in MongoDB, then setting new Value with the old field Name: here is the example :

 db.exemption.update({'ref':163},{$rename: 
 {'request.reference':'request.oldRef'},$set:
 {'request.reference':'00000'}},true,true) 

but heri the error that I got

{ [MongoError: Cannot update 'request.reference' and 'request.reference' at the same time]
  name: 'MongoError',
  code: 16837,
  err: 'Cannot update \'request.reference\' and \'request.reference\' at the same time' }

Is there a possible way to do the update of this field inspite of using 2 queries mongo?

You could do

 db.exemptions.update({ref: <value>}, {
     $unset: {'request.oldRef': 1},
     $set: {'request.reference': <new value>}
 });

It doesn't really rename the field, but it gives you the same result.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM