0

I have a document in a Mongo collection which looks something like this:

{
  _id: [uuid]
  prop: 
    some: "text",
    subprop: ['foo', 'bar']
}

I'd like to avoid having to pull the whole record out, modify it and push it back if that's possible. What update can I run to have 'bar' removed from doc.prop.subprop, not knowing the index it's stored at?

Thanks :)

1
  • 2
    Doesn't $pull do this: db.col.update({}, {$pull:{'prop.subprop': 'bar'}}) ? Commented Dec 18, 2012 at 15:54

1 Answer 1

2

You should be able to do this using the $pull operator, which can pull a specific value from a field. You can use dot notation to get the subprop field:

db.foo.update( { _id : [uuid] }, { $pull : { "prop.subprop" : "bar" } } );

If you want to run the update for multiple documents in the collection, you'll need to set multi : true in your query with the following syntax:

db.foo.update( { _id : [uuid] }, { $pull : { "prop.subprop" : "bar" } }, { multi : true } ); 

This means that the update query will run on all documents that match { _id : [uuid] } in your collection.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect thanks very much :) Tried going through every operator in the docs, but that one must have missed me :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.