I want to rename foo.one
to foo.two
.
Mongodb has a way to rename fields with update $rename:
db.foo.update({}, { $rename: { 'one': 'two' } }, { multi: true })
However, the app will be running during migration, so there could be some new records created with some two
before migration finishes, and if I understand correctly, they'd get overwritten by migration.
Is there any way to migrate without such risks?
CodePudding user response:
Use the filter part of the update call to exclude documents that already have a 'two' field:
db.foo.update({two:{$exists:false}}, { $rename: { 'one': 'two' } }, { multi: true })