How to remove a column from a child collection

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

I tried the following to remove it, but it did not work ... There is no error message, but the LastErrorMessage column still exists.

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

Any ideas what I'm doing wrong?

Another related question, if I make the $ unset command for a column in a very large (millions of rows) collection, mongodb uses the whole ram on the server (as if it is trying to save the entire collection in memory), then the server crashes. Is there a better way to remove columns if there are large collections?

+13
mongodb unset
Feb 13 2018-11-28T00:
source share
2 answers

Updating with an empty parameter does not work. I tried this in the mongo shell and mongoconsole. In mongoconsole, he gave an update error, expecting the array or object to be the first parameter.

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

Try:

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

It worked for me.

Keep in mind that based on documents, $ exists does not use an index, so it will be slower. I suggest adding a parameter that can be added to the index and request it when $ unset is executed.

+13
Feb 14 2018-11-11T00:
source share
β€” -

It looks like you have a couple of problems.

# 1: $unset

As far as I can see, this should work fine. I got the following result in 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 RAM

if I make the $ unset command for a column in a very large collection, mongodb uses all the ram on the server (as if it is trying to store the entire collection in memory)

What MongoDB is trying to do. MongoDB uses memory mapped files. MongoDB will pull out all the data in RAM and allow the operating system to manage virtual memory problems.

So, when you make a query without indexes, you basically ask MongoDB to go through each element of the collection. This is basically a gigantic loop that runs on all of your data, so you need to download everything from disk.

So far this is normal.

... then the server crashes

It is not normal. I ran this type of update command on hundreds of millions of documents without server crashes. Can you provide more details about this issue? Do you have log files?

If so, I would suggest taking your errors into Google groups so that they can help identify the source of the failure. http://groups.google.com/group/mongodb-user

0
Feb. 15
source share



All Articles