Open In App

Mongoose Schema Connection.prototype.collections API

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

The Mongoose Schema API Connection.prototype.collections property of the Mongoose API is used on the Connection object. It allows us to get the details about the collections that are associated with the connection object. Each connection object poses how many collections that information we will get in the hashed format. Let us understand the collections property using an example.

Syntax:

connection.collections;

Parameters: This property does not accept any parameter.

Return Value: This property returns hashed information of the collections associated with the connection object.

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: 

 

Example 1: The below example illustrates the basic functionality of the Mongoose Connection collections property. As we have defined customer model collections property returning information related to that only.

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,
})
  
const Customer = connectionObject.model('Customer'
new mongoose.Schema({
    name: String,
    address: String,
    orderNumber: Number,
}));
  
const result = connectionObject.collections;
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:

 

Example 2: The below example illustrates the basic functionality of the Mongoose Connection collections property. In this example, we have defined two models customer and student. Using the collections property we are able to get the information for both the models.

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,
})
  
const Customer = connectionObject.model('Customer'
new mongoose.Schema({
    name: String,
    address: String,
    orderNumber: Number,
}));
  
const studentSchema = new mongoose.Schema({
    name: {type: String, required: true},
    age: Number,
    rollNumber: {type: Number, required: true}
});
  
const StudentModel = connectionObject.model(
    'Student', studentSchema);
  
const result = connectionObject.collections;
const obj = Object.keys(result);
console.log(obj);


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

node app.js

Output:

[ 'customers', 'students' ]

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads