Skip to content

Instantly share code, notes, and snippets.

@divjotarora
Created December 16, 2020 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divjotarora/992099cef9e2014582fcd5698521f2e3 to your computer and use it in GitHub Desktop.
Save divjotarora/992099cef9e2014582fcd5698521f2e3 to your computer and use it in GitHub Desktop.
Update existing field to value of another field
conn = new Mongo()
db = conn.getDB('test')
coll = db.coll
coll.drop()
try {
print(conn.getDB('admin').runCommand({buildInfo: 1})['version'])
coll.insert({
id: 123,
PreviousRecord: 'old',
CurrentRecord: 'new',
})
print('before updating')
printjson(coll.findOne())
coll.update({id: 123}, {'$set': {'PreviousRecord': '$CurrentRecord'}})
print('after updating')
printjson(coll.findOne())
} catch(e) {
printjson(e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment