How to undo modification/update on document in mongoDB? -


i have mistakenly modified document in collection running command

db.getcollection('persons').update(     // query      {      },      // update      {        "branch":"computer"     },      // options      {         "multi" : false,  // update 1 document          "upsert" : false  // insert new document, if no existing document match query      } );  

it had removed fields in first document except _id field in persons collection.

the above command has resulted in

/* 1 */ {     "_id" : objectid("5527636be4b0b3959623d6f7"),     "branch" : "computer" }  /* 2 */ {     "_id" : objectid("5527637ee4b0b3959623d6f8"),     "name" : "rajnish",     "country" : "bhutan" }  /* 3 */ {     "_id" : objectid("552d133b44aef0215235e3e9"),     "name" : "amol",     "country" : "india" } 

so wanted undo modification. how do it.?

there few things, can do:

  1. delete first entry stores "branch": "computer", with:
db.collection.remove({'_id': objectid("5527636be4b0b3959623d6f7")}) 
  1. you can delete whole collection, , start fresh, with:
db.collection.remove({}) 

or

db.collection.drop() 
  1. even, if don't want delete incorrect entry, can update as:
db.collection.update({'_id': objectid("5527636be4b0b3959623d6f7")}, {'name': 'gaurav', 'country': 'india'}) 
  1. if update, fields specified store , rest cleared out. so, have use $set in:
db.collection.update({'_id': objectid("5527636be4b0b3959623d6f7")},{ $set: {'name': 'gaurav', 'country': 'india'}},{upsert: true}) 

see, helps.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -