Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb Cannot apply $pull/$pullAll modifier to non-array, How to remove array element

Tags:

mongodb

i met a problem about mongodb.

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

above is a test document.

i want to pull comments.name = 2

i do

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

but the console print these message:

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

my mongodb version is 2.0.6

who can help me? Thank you very much

like image 279
user1805188 Avatar asked Mar 10 '13 14:03

user1805188


1 Answers

Your $pull syntax is off, it should be:

db.tt.update({'comments.name': '2'}, {$pull: {comments: {name: '2'}}})
like image 124
JohnnyHK Avatar answered Oct 08 '22 00:10

JohnnyHK