12

Say I have a document that represents books like this:

{
   "_id":"1234567890",
   "title":"Lord Of The Rings",
   "books": {
      "1234567890":{
         "_id":"123456789890",
         "title":"The Two Towers",
         "page_count":{
            "en":6000,
            "de":7000
         }
      },
      "2234567890":{
         "_id":"223456789890",
         "title":"The Return Of The King",
         "page_count":{
            "en":6000,
            "de":7000
         }
      },
   }
}

How would I go about removing the page count for the 2nd book? I tried

{$unset : {"books.2234567890.page_count":""}}

Didn't work. Any ideas?

Thanks a lot

2
  • Can you show the code you're using with that $unset object? Commented Jan 20, 2013 at 14:55
  • I must have missed something because I tried the recommendation from mgoffin below and it all works now. Bizare. Commented Jan 21, 2013 at 3:41

1 Answer 1

13

I took a stab at it and it looks like what you are trying to do should work correctly. I would check your query to locate the proper document to update and make sure it's finding what you want.

> db.books.findOne()
{
        "_id" : "1234567890",
        "title" : "Lord Of The Rings",
        "books" : {
                "1234567890" : {
                        "_id" : "123456789890",
                        "title" : "The Two Towers",
                        "page_count" : {
                                "en" : 6000,
                                "de" : 7000
                        }
                },
                "2234567890" : {
                        "_id" : "223456789890",
                        "title" : "The Return Of The King",
                        "page_count" : {
                                "en" : 6000,
                                "de" : 7000
                        }
                }
        }
}
> db.books.update({'_id': "1234567890"}, {$unset: {'books.2234567890.page_count': ""}})
> db.books.findOne()
{
        "_id" : "1234567890",
        "books" : {
                "1234567890" : {
                        "_id" : "123456789890",
                        "title" : "The Two Towers",
                        "page_count" : {
                                "en" : 6000,
                                "de" : 7000
                        }
                },
                "2234567890" : {
                        "_id" : "223456789890",
                        "title" : "The Return Of The King"
                }
        },
        "title" : "Lord Of The Rings"
}
>
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I just wanted to be sure that $unset was the right operation and I was referencing the document and nested object the right way. There must be something else wrong, I'll follow your suggestion, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.