MongoDB Update for array of object -


i having following document in mongodb

 {   "_id" : objectid("521aff65e4b06121b688f076"),   "uuid" : "160597270101684",   sessionid" : "160597270101684.1",   "stamps" :            {             "currentvisit" : "1377500985",             "lastvisit" : "1377500985"    },    visits : [             {              "page":"google.com",              "method": "get"              }             ]  } 

requirement:

if uuid , sessionid not present insert document above otherwise have push object visits array.

any greatful.

mongodb supports upsert option on update updates matching document if exists, , inserts new document if doesn't exist. in mongodb 2.4+ can use $setoninsert operator further tweak set fields only if upsert performs insert.

db.test.update({      uuid: "160597270101684",      sessionid: "160597270101684.1"    }, {      $setoninsert: {       stamps: {         currentvisit: "1377500985",          lastvisit: "1377500985"       }     },     $push:{       visits: {         page: "google.com",         method: "get"       }     }   }, { upsert:true }) 

so in above example, $push visits occur $setoninsert stamps occur if matching document doesn't exist.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -