开发者

Removing specific sub-document from collection in MongoDB using MongoVUE

Removing a 'key' from a document with MongoVUE is easy - just right click in view mode and select 'remove'.

However, no such option seems to exist if you want to delete a whole subdocument from a开发者_StackOverflow社区 colletion.

Does anyone know if there is a quick/simple way to delete subdocuments from collections using the GUI?(i.e. not resorting to queries)



it's a bit tricky, because you can't delete a key for a whole collection just by clicking the GUI. select your collection where you want to delete the key and click the update button. Now mongoVUE will show the update view.
In the panel 'Enter Find Json' you have to put the following code:
{ "keyToDelete": { $exists : true } }

On the other panel 'Enter Update Json' put the following:
{ $unset : { "keyToDelete" : 1} }

now click the 'Update'-Button to run the query and the key 'keyToDelete' won't appear in the selected collection anymore.

so far hiroorih


It is indeed tricky, especially for people new to MongoDB (and JSON) like myself. I was also trying to delete specific sub documents from MongoVUE but I couldn't figure out how. Finally I found the solution and I wanted to share it here in case other people run into the same problem.

I have this document (User):

{
  "_id" : ObjectId("510d3e719d0d3627ec73abe4"),
  "Name" : "John Doe",
  "Country" : "USA",
  "Highscores" : [{
      "Score" : 15,
      "DateStamp" : ISODate("2013-02-02T11:35:51.905Z")
    }, {
      "Score" : 19,
      "DateStamp" : ISODate("2013-02-02T11:36:04.886Z")
    }, {
      "Score" : 40,
      "DateStamp" : ISODate("2013-02-02T11:36:21.714Z")
    }]
}

I want to delete scores above 20, i.e. only the last highscore and leave the list with two sub documents. I can find my document (the entire User document) by this query:

{ "Highscores.Score": { $gt: 20 } }

But if I open a Remove window in MongoVUE and enter the search query it will delete the entire User, including all highscores. I couldn't get $unset to delete the sub document and after some searching I found that this update JSON was the key:

{ $pull : { Highscores : { Score : { $gt: 20 } } } }

Hope it helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜