Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting MongoDB's write concern in shell / shell script

Tags:

shell

mongodb

I'm trying to populate a collection within MongoDB's shell. As I understand, using a relaxed Write Concern can speed up the process a lot. I'm talking about the documentation on how Write Concerns work but all it talks about is how they work and not how to set them (it says this is set in the driver). And I've seen documents for different drivers and how it is set in them but I can't seem to find how Write Concerns are set in MongoDB's shell itself! Does anyone know how to set them from the shell?

[UPDATE]

To populate my database I used a .js file and gave it to mongo command like this:

mongo ./test.js

And here's the content of test.js file:

for (var i=0; i<1000000; i++) {
    db.kingdom.insert([
        { "counter" : 0, "last_update" : new Date() }
        , { "counter" : 0, "last_update" : new Date() }
        ...
        , { "counter" : 0, "last_update" : new Date() }
    ]);
}

The data array goes for 100 objects of the same structure.

like image 792
Mehran Avatar asked Dec 05 '25 10:12

Mehran


1 Answers

You can specify a write concern for each insert/update operation:

db.blogs.insert({title: 'new blog post'}, {writeConcern: {j: true}})

like image 102
Oliver Wolf Avatar answered Dec 07 '25 15:12

Oliver Wolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!