Create a collection with node.js

Which mongodb version you are using. I tried this but still not working. Please can you help. I am not even getting any error. Getting success print but db not created.

Hi @Anand_Vaidya1, your problem might not be a connection issue at all. what do you get if you run the code in the question itself?

I get success log but when I go and check through MongoDb Compass no DB is created.

const { MongoClient } = require('mongodb');
var ObjectId = require('mongodb').ObjectId;

const url = 'mongodb://127.0.0.1:27017';
const client = new MongoClient(url);

// Database Name
const dbName = 'myProject';

async function main() {
  // Use connect method to connect to the server
  await client.connect();
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  const collection = db.collection('documents');

  client.close();
  // the following code examples can be pasted here...

  return 'done.';
}
main();

Alright now, besides not being a connection problem, your issue is actually not an issue at all but a small detail you missed on the way (that can happen to any of us at any time):

  • with this example code, if a collection does not exist then no collection is created unless you insert a document before closing the client connection.

follow on with tutorials to see how you insert documents, then you should see your newly created collection and the data in it.

if you happen to have other issues, create a new topic and give as many details as possible.

cheers :wink:

Thanks a lot. This worked but I need to only create empty tables without default entries. I am providing a library for other modules where my functions need to be clear and crisp.
Is there a way where I can just do specific actions.
E.g.
CreateDB() : THis function will only create db.
CreateTable(): This function will only create table/ collection.
WriteTable(), ReadTable() etc.

Hi @Anand_Vaidya1 welcome to the community!

I need to only create empty tables without default entries

The resource you’re looking for is probably https://www.mongodb.com/docs/manual/reference/method/db.createCollection/

In short, MongoDB uses a very different concept to SQL in managing your data.

Unlike the typical SQL workflow, MongoDB works very differently. Here, a “collection” is a collection of JSON-like documents. The “schema” here is more like uniformity of keys and values of the documents inside the collection. You don’t define the schema beforehand, the collection will be created automatically when you insert a new document into a non-existing collection, and you can have two documents that look very different from each other inside a single collection. It’s termed “flexible schema” in MongoDB.

This is quite different to SQL’s concept of tables and schemas, where you have to define a schema for every table. Thus, the concept of creating functions of CreateDB(), CreateTable(), WriteTable() etc. don’t have direct equivalents in MongoDB.

If you need specific help on what you need to do, please provide more details. e.g. what’s the WriteTable() function supposed to do? What’s the overall function of your application? And other relevant details.

Best regards
Kevin

4 Likes