0

I have a JSON document like

{
"branch": [
    {
        "section": [
            {
                "sub": "edc",
                "time": "one hour",
                "frequency": "3"
            },
            {
                "sub": "bee",
                "time": "two hours",
                "frequency": "4"
            }
        ]
    },
    {
        "section": [
            {
                "sub": "ss",
                "time": "one hour",
                "frequency": "2"
            },
            {
                "sub": "ms",
                "time": "two hours",
                "frequency": "5"
            }
        ]
    }
]

}

Now I want to delete

{
  "sub": "edc",
  "time": "one hour",
  "frequency": "3"
}

using "sub":"edc" from the following collection

I want the query to perform changes in mongo db

Noel
  • 10,152
  • 30
  • 45
  • 67
user2189941
  • 775
  • 2
  • 7
  • 10
  • Is `branch` the collection containing `section` documents? – Emii Khaos May 07 '13 at 09:48
  • "branch" is not a collection. The above json document is in the collection say "college" in mongo db. I want to update the database where I could remove { "sub": "edc", "time": "one hour", "frequency": "3" } the following – user2189941 May 07 '13 at 11:10

1 Answers1

2

You need to use $pull, although i've not done it with nested array.

See http://docs.mongodb.org/manual/reference/operator/pull/

Something like: (but you'll need to test it)

db.yourcoll.update( { "branch.section.sub": 'edu' }, { $pull: { "branch.section.sub": 'edu' } } )

This is a similar question:

How to remove an element from a doubly-nested array in a MongoDB document

Community
  • 1
  • 1
sambomartin
  • 6,663
  • 7
  • 40
  • 64