0

According to tie piece of documentation if I run these upon mongosh:

use myDB
db.myNewCollection1.insertOne( { x: 1 } )

Will automatically create a myDb database and will a collection myNewCollection1.

Therefore if upon my nodejs run:

const {MongoClient, ServerApiVersion} = require('mongodb');

// Connectionstring
const uri = 'mongodb://root:[email protected]:27017/';

// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
    serverApi: {
        version: ServerApiVersion.v1, strict: true, deprecationErrors: true,
    }
});
client.connect({poolSize:10});
const database = client.db('myDb');
const collection = database.collection('myNewCollection1')

collection.updateOne({"var":value})

I mean if I run the nodejs code above will automatically create the database myDb and collection myNewCollection1 as would be done via mongosh?

1
  • Sounds like a pretty easy thing to test, especially if you already have the code written. I look forward to upvoting an answer from you with the findings Jun 22, 2023 at 17:16

1 Answer 1

0

The nodejs driver will always create the collection if one does not already exist when a document is inserted, as the documentation suggests:

You can insert a document into a collection using the collection.insertOne() method. To insert a document, define an object that contains the fields and values that you want to store. If the specified collection does not exist, the insertOne() method creates the collection.

If you wish to tweak it, for example, make it time-series, cropped or change the default string collation, you should create it manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.