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)?
collection. updateMany() can be used inside multi-document transactions.
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.
You can use updateMany for this too:
try {
db.<collection>.updateMany( {}, {$set: { LowerCaseAddress:
item.Address.toLowerCase() } } );
} catch(e) {
print(e);}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With