How to Check if a Collection Exists in MongoDB Using NodeJS

MD Aminul Islam Feb 02, 2024
  1. Check if a Collection Exists in MongoDB Using NodeJS
  2. An Example of Checking the Existence of a Collection
How to Check if a Collection Exists in MongoDB Using NodeJS

When working with large-scale databases in MongoDB, it’s important to check if a collection already exists in the database before creating a new collection.

In this article, we are going to check if a collection exists on a MongoDB database, and also we will look at an example that is relevant to the topic to make the topic easier. For this purpose, we are going to use Node.js.

Check if a Collection Exists in MongoDB Using NodeJS

To find if a collection exists, we first need to connect with the MongoDB server, and then we need to query like this:

  dbs.collection("Your_collection_name").find().toArray(function(err, res) {
    if ( res.length > 0 )
      {
          console.log("Exist!!!");
      }
      else
      {
          console.log("Not Exist!!!");
      }
      db.close();
  });

The idea here is to collect the documents inside a collection. Now, if the collection doesn’t exist, the function will return an error which means the collection doesn’t exist.

Also, the result will remain empty for a collection that doesn’t exist.

An Example of Checking the Existence of a Collection

In our below example, we will see how we can check the existence of a collection using Node.js. The JavaScript code for this purpose is like the following:

var MyClient = require('mongodb').MongoClient;
var URL = 'mongodb://localhost:27017/';

MyClient.connect(URL, function(err, db) {
  if (err) console.log(err);
  var dbs = db.db('Mydb');
  dbs.collection('abc').find().toArray(function(err, res) {
    if (res.length > 0) {
      console.log('Exist!!!');
    } else {
      console.log('Not Exist!!!');
    }
    db.close();
  });
});

In the above example code, we first connect with the MongoDB server. Then, we selected a specific database.

After, we checked a collection’s existence which we have discussed already.

Now, after running the above example code, you will get an output like the below:

Not Exist!!!

Please note that we executed the code through Node.js, so you should install Node.js and the MongoDB package to execute the JavaScript code. After installing Node.js, you can install MongoDB in your directory using the npm install mongodb command.

Please note that the commands shown in this article are for the MongoDB database and the command needs to be run on the MongoDB console.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - MongoDB Collection