2013-01-20 33 views
3

说我有一个代表书籍像这样的文件:如何在MongoDB中删除了深刻嵌套对象

{ 
    "_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 
     } 
     }, 
    } 
} 

我怎么会去的第二本书移除页面计数?我试过

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

没有工作。有任何想法吗?

非常感谢

+0

你可以使用'$ unset'对象显示你正在使用的代码吗? – JohnnyHK

+0

我一定错过了一些东西,因为我尝试了下面的mgoffin的建议,现在一切正常。 Bizare。 – JohnE

回答

7

我参加了一个刺它,它看起来像你正在尝试做的应该正常工作。我会检查你的查询来找到适当的文件进行更新,并确保它找到你想要的。

> 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" 
} 
> 
+1

好吧,我只是想确定$ unset是正确的操作,并且我正在以正确的方式引用文档和嵌套对象。一定有其他的错误,我会按照你的建议,非常感谢! – JohnE