Open In App

Mongoose Query.prototype.merge() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Mongoose Query API.prototype.merge() method of the Mongoose API is used on the Query objects. It allows us to merge one query with another query object. With the help of this method we can merge the conditions, field selection, and options of one query object with another query object. Let us understand the merge() method using an example.

Syntax:

query.merge( source );

Parameters: This method accepts a single parameter as discussed below:

  • source: It is used to specify the query object to be merged.

Return Value: This method returns query object on which we can call callback function.

Setting up Node.js application:

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 documents are present in the collection.

 

Example 1: In this example, we are illustrating the functionality of merge() method, where we are merging sourceQuery object to the query object. 

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 query = Customer.find();
const sourceQuery = new mongoose.Query();
sourceQuery.ne("orderNumber", 15);
query.merge(sourceQuery);
query.exec((error, result) => {
    if (error) {
        console.log("Error -", error);
    } else {
        console.log("Result -", 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:

Result - [
  {
    _id: new ObjectId("639ede899fdf57759087a653"),
    name: 'Aditya',
    address: 'Mumbai',
    orderNumber: 20,
    __v: 0
  },
  {
    _id: new ObjectId("63bcfcc2876922405349b69d"),
    name: 'Bhavesh',
    address: 'Mhow',
    orderNumber: 65,
    __v: 0
  }
]

Example 2: In this example, we are illustrating the functionality of merge() method, where we are merging sourceQuery object to the query object. At the end, with the merging functionality conditions, filters, options are also being merged and we are able to see the result from the both the query conditions. 

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 query = Customer.find();
query.nin('name', ["Chintu"]);
const sourceQuery = new mongoose.Query();
sourceQuery.ne("orderNumber", 65);
query.merge(sourceQuery);
query.then((res => {
    console.log(res);
})).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:

[
  {
    _id: new ObjectId("639ede899fdf57759087a653"),
    name: 'Aditya',
    address: 'Mumbai',
    orderNumber: 20,
    __v: 0
  }
]

Reference: https://mongoosejs.com/docs/api/query.html#query_Query-merge



Last Updated : 13 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads