mongodb - Mongo DB-adding Json object to existing document without creating a new entry(not an array) -


i have json object in mongodb inserted.

 {     "appname":"abcd",     "appid":"e3456",     "modules":{       "sales":{          "apis":{             "get sales":{                "method":"get",                "access_ctrl_level":"admin",                "req_fields":[                 ],                "url":"url_to_api",                "error_code":[                   {                      "code":502,                      "desc":"no authentication"                   }                ]             }          }       }   } } 

now want add object "sales.apis".

 "get sales details":{                "method":"get",                "access_ctrl_level":"admin",                "req_fields":[                   "saleno"                ],                "url":"url",                "error_code":[                   {                      "code":502,                      "desc":"no authentication"                   }                ]             } 

i used below code find , modify document insert new object collection

db.metadata.findandmodify({query: { $and:[{modules:{sales:{apis:{"get sales":{method:"get"}}}}},{"appname":"erp"}] },     update: {$set:{modules:{sales:{apis:{"get sales details":{method:"post","access_ctrl_level":"admin","url":"url"}}}}}},     upsert: true }) 

but creating new entry in metadata document. want insert under "modules.apis.get sales details" in same document idea?

please try through dot notation

to access field of embedded document dot-notation, concatenate embedded document name dot (.) , field name, , enclose in quotes:

findandmodify({         query: { "modules.sales.apis.get sales.method":"get",                   "appname":"erp"} ,         update: {                $set:{"modules.sales.apis.get sales details":                      {"method":"post",                      "access_ctrl_level":"admin",                      "url":"url"}}},         upsert: true })   

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -