Open In App

Mongoose Connection.prototype.createCollection() Function

Last Updated : 22 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Schema API Connection.prototype.createCollection() method of the Mongoose API is used on the Connection object. It allows us to create collection explicitly by providing the name of the collection and options to related to it. Let us understand the createCollection() method using an example.

Syntax:

connection.createCollection( collection, options, callback );

Parameters: This method accept three parameters as described below:

  • collection: It is used to specify the name of the collection.
  • options: It is used to specify various properties for the object.
  • callback: It is used to specify the callback function.

Return Value: This method returns a promise if we do not provide any callback function.

Setting up Node.js Mongoose Module:

Step 1: Create a Node.js application using the following command.

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command.

npm install mongoose

Project Structure: The project structure will look like this.

 

Database Structure: The database structure will look like this, the following database present in the MongoDB.

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection createCollection() method, using then and catch block. In this example, we are creating gfgexample collection on connection object for geeksforgeeks database.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
connectionObject
    .createCollection('gfgcollection')
    .then(result => {
        console.log(result)
    }).catch(err => {
        console.log(err)
    });


Step to run the program: To run the application execute the below command from the root directory of the project.

node app.js

Output: GUI Representation of the Database using Robo3T GUI tool.

Mongoose Schema API Connection.prototype.createCollection() Function

Mongoose Schema API Connection.prototype.createCollection() Function

Example 2: The below example illustrates the basic functionality of the Mongoose Connection createCollection() method, using callback function. In this example, we are creating newtest collection on connection object for the geeksforgeeks database.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
  
const connectionObject =
    mongoose.createConnection(URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    });
  
connectionObject
    .createCollection('newtest', (error, result) => {
        if (error) {
            console.log(error);
        } else {
            console.log(result);
        }
    })


Step to run the program: To run the application execute the below command from the root directory of the project.

node app.js

Output: GUI Representation of the Database using Robo3T GUI tool.

Mongoose Schema API Connection.prototype.createCollection() Function

Mongoose Schema API Connection.prototype.createCollection() Function

Reference: https://mongoosejs.com/docs/api/connection.html#connection_Connection-createCollection



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads