2016-04-08 22 views
1

Angenommen, Sie die folgenden Dokumente in meiner Sammlung haben:Update-Objekt in ein Array MongoDB

{foo:[{ 
    bar1:{_id:1, active: true}, 
    bar2:{_id:2, active: true} 
    },{}] 
} 

Sie query:

db.test.update({$or: [ 
    {'foo.bar1._id': 1)}, 
    {'foo.bar2._id': 1} 
    ] 
}, 
{$set: {'foo.?????.active': false}}) 

Ergebnis:

{foo:[{ 
    bar1:{_id:1, active: false}, 
    bar2:{_id:2, active: true} 
    },{}] 
} 

Antwort

0

Try this:

db.test.update({"foo.0.bar1._id" : 1}, {$set : {"foo.0.bar1.active" : "false"}}) 

Oder

db.test.update({"foo.0.bar1.active" : "true"}, {$set : {"foo.0.bar1.active" : "false"}}) 

hier foo.0 ist Index.

Danke.