2017-06-13 30 views
0

参数I具有这个文档:如何编写MongoDB的查询?我需要设置在阵列

{ 
    '_id': 123456, 
    'name': 'myName', 
    'state':[{'user_id':1,user_state:1},{'user_id':11,user_state:2}] 
} 

我需要设置一个更日提交的, 'IS_ACTIVE':1,在状态阵列对象,其中 “_id” == 123456,USER_ID == 11

因此,它应该是这样的:

{ 
    '_id': 123456, 
    'name': 'myName', 
    'state': [{'user_id':1,user_state:1}, 
       {'user_id':11,user_state:2,'is_active':1}] 
} 

但我坚持查询:

{_id: new ObjectID(123456)}, {$set:{'state':???}} 

在此先感谢

回答

1

试试这个,

db.colection_name.update({_id: ObjectId("123456"), "state.user_id": 11}, { $set:{ "state.$.is_active" : 1 }}) 
+0

现在会尝试。谢谢! – Serhiy

+0

worked.thanks。标记了你的答案。 – Serhiy

+0

高兴地帮助:) –