架构是这样的
{
"_id":ObjectId('5aaa41f96f69440f82ef45a7')
"name":"test",
"password" : "1234",
"userInfo" : {
"userName" : "usha",
"organization" : [1,2]
}
}
如果我正在使用这样的查询,那么它将替换userInfo对象,而不是更新它。
db.user.update({"_id":ObjectId('5aaa41f96f69440f82ef45a7')},{$set :{"userInfo":{"userName":"kanha"}}})
after using this query it is giving this result
{
"_id":ObjectId('5aaa41f96f69440f82ef45a7')
"name":"test",
"password" : "1234",
"userInfo" : {
"userName" : "kanha"
}
}
它正在从userInfo
中删除组织预期结果
{
"_id":ObjectId('5aaa41f96f69440f82ef45a7')
"name":"test",
"password" : "1234",
"userInfo" : {
"userName" : "kanha",
"organization" : [1,2]
}
}
答案 0 :(得分:0)
你可以尝试
db.user.update({'_id':ObjectId('5aaa41f96f69440f82ef45a7')}, {$set:
{
'userInfo.$.userName': 'kanha'
}
});
答案 1 :(得分:0)
db.users.update({_id: '5aaa41f96f69440f82ef45a7'}, { '$set': {"userInfo.$.userName" : 'usha'})
它也适用于嵌套数组。