如何从子集合中删除列

发布于 2024-10-17 12:09:29 字数 463 浏览 7 评论 0原文

我在 MongoDB 中有一个名为 CrawlUser 的集合。它有一个名为 CrawlStatuses 的列表,它是 CrawlStatus 对象的列表。 CrawlStatus 有一个名为 LastErrorMessage 的属性,我想从集合中删除它。

我尝试执行以下操作来删除它,但它不起作用...没有给出错误消息,但 LastErrorMessage 列仍然存在。

db.CrawlUser.update( {}, { $unset: { "CrawlStatuses.LastErrorMessage": 1 } }, false, true);

有什么想法我做错了吗?

另一个相关问题,如果我对非常大(数百万行)的集合中的一列执行 $unset 命令,mongodb 会耗尽服务器上的所有内存(就好像它试图将整个集合存储在内存中一样) ),然后服务器崩溃。当您有大量集合时,是否有更好的方法来删除列?

I have a collection in MongoDB called CrawlUser. It has a list called CrawlStatuses, which is a list of CrawlStatus objects. CrawlStatus has a property called LastErrorMessage which I want to remove from the collections.

I tried to do the following to remove it but it didn't work... No error message given, but the LastErrorMessage column is still there.

db.CrawlUser.update( {}, { $unset: { "CrawlStatuses.LastErrorMessage": 1 } }, false, true);

Any ideas what I'm doing wrong?

One other related question, if I do the $unset command on a column in a collection that is very large (millions of rows), mongodb uses up all of the ram on the server (as if it's trying to store the entire collection in memory), then the server crashes. Is there a better way to remove columns when you have large collections?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

垂暮老矣 2024-10-24 12:09:29

使用空参数的更新似乎不起作用。我在 mongo shell 和 mongoconsole 中尝试过。在 mongoconsole 中,它给出了有关更新的错误,期望第一个参数是数组或对象。

但是,您可以使用 $exists 查找查询执行相同的操作。

尝试:

`db.CrawlUser.update( {CrawlStatuses:{$exists:true}}, { $unset: { "CrawlStatuses.LastErrorMessage": 1 } }, false, true);`

这对我有用。

请记住,根据文档, $exists 不使用索引,因此它会更慢。我建议添加一个参数,您可以在其上添加索引并在执行 $unset 时查询它。

The update with the empty parameter doesn't seem to work. I tried it in the mongo shell and mongoconsole. In the mongoconsole it gave an error about update expecting the first parameter to be an array or an object.

However, you can do the same thing using the $exists find query.

Try:

`db.CrawlUser.update( {CrawlStatuses:{$exists:true}}, { $unset: { "CrawlStatuses.LastErrorMessage": 1 } }, false, true);`

That worked for me.

Keep in mind that based on the docs, $exists doesn't use an index, so it will be slower. I suggest adding a parameter that you can add an index on and query it when doing the $unset.

天赋异禀 2024-10-24 12:09:29

看起来您这里有几个问题。

#1:$unset 命令

据我所知,这应该可以正常工作。我的测试得到以下输出:

MongoDB shell version: 1.6.5
connecting to: test
> db.foo.save( { _id : 1, status : { err : 'blah', y : 1 } } )
> db.foo.save( { _id : 2, status : { err : 'blahblah', y : 5 } } )
> db.foo.find()
{ "_id" : 1, "status" : { "err" : "blah", "y" : 1 } }
{ "_id" : 2, "status" : { "err" : "blahblah", "y" : 5 } }
> db.foo.update( { }, { $unset : { "status.err" : 1 } }, false, true )
> db.foo.find()
{ "_id" : 1, "status" : { "y" : 1 } }
{ "_id" : 2, "status" : { "y" : 5 } }

#2:耗尽 RAM

如果我对非常大的集合中的列执行 $unset 命令,mongodb 会耗尽服务器上的所有内存(就好像它试图将整个集合存储在内存中一样)

这正是 MongoDB 正在尝试做的事情。 MongoDB 使用内存映射文件。 MongoDB 会将所有数据提取到 RAM 中,并让操作系统管理虚拟内存问题。

因此,当您执行没有索引的查询时,您基本上是在要求 MongoDB 遍历集合中的每个项目。它基本上是一个对所有数据进行操作的巨大 for 循环,因此这需要从磁盘加载所有内容。

到目前为止,这一切都是正常的。

...然后服务器崩溃

这不正常。我已经对数亿个文档运行了这种类型的更新命令,而没有使服务器崩溃。您能否提供有关此问题的更多详细信息?你有日志文件吗?

如果是这样,我建议将您的错误带到 Google 网上论坛,以便他们可以帮助确定崩溃的根源。
http://groups.google.com/group/mongodb-user

Looks like you have a couple of issues here.

#1: The $unset command

As far as I can see, this should work just fine. I got the following output on my test:

MongoDB shell version: 1.6.5
connecting to: test
> db.foo.save( { _id : 1, status : { err : 'blah', y : 1 } } )
> db.foo.save( { _id : 2, status : { err : 'blahblah', y : 5 } } )
> db.foo.find()
{ "_id" : 1, "status" : { "err" : "blah", "y" : 1 } }
{ "_id" : 2, "status" : { "err" : "blahblah", "y" : 5 } }
> db.foo.update( { }, { $unset : { "status.err" : 1 } }, false, true )
> db.foo.find()
{ "_id" : 1, "status" : { "y" : 1 } }
{ "_id" : 2, "status" : { "y" : 5 } }

#2: Using up RAM

if I do the $unset command on a column in a collection that is very large, mongodb uses up all of the ram on the server (as if it's trying to store the entire collection in memory)

That's exactly what MongoDB is trying to do. MongoDB uses memory-mapped files. MongoDB will pull all of the data into RAM and let the operating system manage the virtual memory concerns.

So when you do a query with no indexes, you're basically asking MongoDB to walk through every item in the collection. It's basically a giant for loop operating on all of your data, so this is going to require loading everything from disk.

Up to now, this is all normal.

... then the server crashes

This is not normal. I have run this type of update command on hundreds of millions of documents without crashing the server. Are you able to provide any more detail on this problem? Do you have log files?

If so, I would suggest taking your bugs to the Google Groups, so they can help identify the source of the crash.
http://groups.google.com/group/mongodb-user

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文