Mongodb Unable to apply $ pull / $ pullAll modifier for non-array, how to remove array element

I ran into mongodb problem.

db.tt.find() { "_id" : ObjectId("513c971be4b1f9d71bc8c769"), "name" : "a", "comments" : [ { "name" : "2" }, { "name" : "3" } ] } 

above is a test document.

I want to pull out comments.name = 2

i do

 db.tt.update({'comments.name':'2'},{'$pull':{'comments.$.name':'2'}}); 

but the console will print the following message:

Cannot apply $ pull / $ pullAll modifier to non-array

my version is mongodb 2.0.6

who can help me? Thank you very much

+4
source share
1 answer

The $pull syntax is disabled, it should be:

 db.tt.update({'comments.name': '2'}, {$pull: {comments: {name: '2'}}}) 
+6
source

All Articles