7

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

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
user1805188
  • 83
  • 2
  • 7

1 Answers1

12

Your $pull syntax is off, it should be:

db.tt.update({'comments.name': '2'}, {$pull: {comments: {name: '2'}}})
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • db.col.find() { "_id" : ObjectId("d53d7810352eaec7c154b711"), "name" : "Amount", "type" : "text", "message" : { "test value" : 1, "test value1" : 6 } } db.col.update({"_id":ObjectId("d53d7810352eaec7c154b711")},{$pull :{"message": {"test value" : 1}}}); i tried this it is not working for me – Ravi Kumar Jul 12 '15 at 12:57
  • @RaviKumar Please post that as a new question if you're still having trouble. – JohnnyHK Jul 14 '15 at 15:34
  • With what this "{$pull: {comments: {name: '2'}}}" can be replaced,to make it accept string as question poster was trying. – Manish Sharma Feb 04 '21 at 05:33