Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new property to each document in a large collection

Using the mongodb shell, I'm trying to add a new property to each document in a large collection. The collection (Listing) has an existing property called Address. I'm simply trying to add a new property called LowerCaseAddress which can be used for searching so that I don't need to use a case-insensitive regex for address matching, which is slow.

Here is the script I tried to use in the shell:

for( var c = db.Listing.find(); c.hasNext(); ) {
   var listing = c.next();
   db.Listing.update( { LowerCaseAddress: listing.Address.toLowerCase() });
}

It ran for ~6 hours and then my PC crashed. Is there a better way to add a new property to each documentin a large collection (~4 million records)?

like image 586
Justin Avatar asked May 31 '11 13:05

Justin


People also ask

What option you will set to update the multiple documents in the collections?

collection. updateMany() can be used inside multi-document transactions.

How many documents can be created in collections?

If you specify a maximum number of documents for a capped collection using the max parameter to create, the limit must be less than 2^32 documents. If you do not specify a maximum number of documents when creating a capped collection, there is no limit on the number of documents.


1 Answers

You can use updateMany for this too:

try {
 db.<collection>.updateMany( {}, {$set: { LowerCaseAddress: 
 item.Address.toLowerCase() } } );
} catch(e) { 
  print(e);}
like image 151
timelf123 Avatar answered Sep 27 '22 21:09

timelf123