Skip to content

Instantly share code, notes, and snippets.

@divjotarora

divjotarora/main.js Secret

Created Dec 16, 2020
Embed
What would you like to do?
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