简体   繁体   English

使用MongoVUE从MongoDB中的集合中删除特定的子文档

[英]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'. 使用MongoVUE从文档中删除“键”非常容易-只需在查看模式下右键单击并选择“删除”即可。

However, no such option seems to exist if you want to delete a whole subdocument from a colletion. 但是,如果您要从一个集合中删除整个子文档,似乎不存在这样的选项。

Does anyone know if there is a quick/simple way to delete subdocuments from collections using the GUI? 有谁知道是否有使用GUI从集合中删除子文档的快速/简单方法? (ie 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. 这有点棘手,因为您不能仅通过单击GUI来删除整个集合的键。 select your collection where you want to delete the key and click the update button. 选择要删除密钥的集合,然后单击更新按钮。 Now mongoVUE will show the update view. 现在mongoVUE将显示更新视图。
In the panel 'Enter Find Json' you have to put the following code: 在“ Enter Find Json”面板中,您必须输入以下代码:
{ "keyToDelete": { $exists : true } }

On the other panel 'Enter Update Json' put the following: 在另一个面板上,输入“ Enter Update Json”,输入以下内容:
{ $unset : { "keyToDelete" : 1} }

now click the 'Update'-Button to run the query and the key 'keyToDelete' won't appear in the selected collection anymore. 现在单击“更新”按钮以运行查询,键“ keyToDelete”将不再出现在所选集合中。

so far hiroorih 到目前为止hiroorih

It is indeed tricky, especially for people new to MongoDB (and JSON) like myself. 这确实很棘手,特别是对于像我这样刚接触MongoDB(和JSON)的人而言。 I was also trying to delete specific sub documents from MongoVUE but I couldn't figure out how. 我也试图从MongoVUE删除特定的子文档,但我不知道如何。 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, ie only the last highscore and leave the list with two sub documents. 我想删除高于20的分数,即仅删除最后的高分,并在列表中保留两个子文档。 I can find my document (the entire User document) by this query: 我可以通过以下查询找到我的文档(整个User文档):

{ "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. 但是,如果我在MongoVUE中打开“删除”窗口并输入搜索查询,它将删除整个用户,包括所有高分。 I couldn't get $unset to delete the sub document and after some searching I found that this update JSON was the key: 我无法获得$ unset来删除子文档,经过一番搜索之后,我发现此更新JSON是关键:

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

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM