Update a document (quickly) - MongoDB -
consider mongodb document:
{ "_id" : "rma.103", "official_name" : "real madrid club de fùtbol", "country" : "spain", "started_by" : { "day" : 6, "month" : 3, "year" : 1902 }, "stadium" : { "name" : "santiago bernabeu", "capacity" : 85454 }, "palmarès" : { "la liga" : 32, "copa del rey" : 19, "supercopa de espana" : 9, "uefa champions league" : 10, "uefa europa league" : 2, "uefa super cup" : 2, "fifa club world cup" : 4 }, "uniform" : "white" } i forgot insert important information of team: the common name. so, updated document:
[1] db.team.update({_id:"rma.103"}, {$set:{common_name:"real madrid"}}) in way, new information added @ end of document, instead want after official_name:
{ "_id" : "rma.103", "official_name" : "real madrid club de fùtbol", "common_name" : "real madrid" ....... ....... ....... } now, know updating document following method, have common name of team in right location:
db.team.update( {_id:"rma.103"}, {$set:{ "_id" : "rma.103", "official_name": "real madrid club de fùtbol", common_name:"real madrid", "country" : "spain", "started_by" : { "day" : 6, "month" : 3, "year" : 1902 }, "stadium" : { "name" : "santiago bernabeu", "capacity" : 85454 }, "palmarès" : { "la liga" : 32, "copa del rey" : 19, "supercopa de espana" : 9, "uefa champions league" : 10, "uefa europa league" : 2, "uefa super cup" : 2, "fifa club world cup" : 4 }, "uniform" : "white", "common_name" : "real madrid" }}) i have update lot of documents , kind of operation difficult , boring shell of prompt. there faster methods update? example possible change update method of [1]?
no, not really. should not have have in specific order. when querying application, won't care order values in. example:
db.collection("team").find({offical_name:"real madrid club de fùtbol"},function(err,results) { /*gets first result (should one) , prints common name*/ console.log(results[0].common_name); }); if wondering why not have way order human eyes, must remember databases not designed interacted directly. supposed in conjunction application uses them , presents data in human friendly manner.
i hope answered question sufficiently, if not please comment below , explain more.
Comments
Post a Comment