Skip to content
Related Articles

Related Articles

How does Query.prototype.merge() works in Mongoose ?

View Discussion
Improve Article
Save Article
  • Last Updated : 12 Mar, 2021
View Discussion
Improve Article
Save Article

The Query.prototype.merge() function is used to merge another Query or conditions object into the current query for which this function is used i.e this query. When a Query is passed, conditions, field selection and options are merged.
Syntax: 
 

Query.prototype.merge()

Parameters: This function has one source parameter of Object/Query type.
Return Value: This function returns Query Object.

Mongoose Installation:

npm install mongoose

After installing the mongoose module, you can check your mongoose version in command prompt using the command. 

npm mongoose --version

After that, you can just create a folder and add a file for example, index.js as shown below.

Database: The sample database used here is shown below: 
 

Example 1:

index.js




const mongoose = require('mongoose');
  
// Database connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = User.find({age: { $gte:0 }})
  
console.log("Before merge function")
query.exec(function(err, res){
    if(err) console.log(err)
    else console.log(res)
});
  
setTimeout(function() {
     
   query.merge({name:"Gourav"})
  
   console.log("After merge function")  
   query.exec(function(err, res){
     if(err) console.log(err)
     else console.log(res)
   });
  
}, 1000);

The project structure will look like this: 
 

Run index.js file using below command: 

node index.js

Output: 

Before merge function
[
{ _id: 5ebb9129a99bde77b2efb809, name: ‘Gourav’, age: 10, __v: 0 },
{ _id: 5ebc3669a99bde77b2efb9ba, name: ‘Lalit’, age: 25, __v: 0 },
{ _id: 5ebc367da99bde77b2efb9bf, name: ‘Piyush’, age: 5, __v: 0 },
{ _id: 5ebd345f5d2d8a3534b2f391, name: ‘Manish’, age: 34, __v: 0 }
]
After merge function
[ { _id: 5ebb9129a99bde77b2efb809, name: ‘Gourav’, age: 10, __v: 0 } ]

Example 2:

index.js




const express = require('express');
const mongoose = require('mongoose');
const app = express()
  
// Database connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const User = mongoose.model('User', { 
    name: { type: String },
    age: { type: Number }
});
  
const query = User.find({age: { $gte: 1}})
  
console.log("Before merge function")
query.exec(function(err, res){
    if(err) console.log(err)
    else console.log(res)
});
  
setTimeout(function() {
     
   query.merge({name:"Gourav"})
  
   console.log("After merge function")  
   query.exec(function(err, res){
     if(err) console.log(err)
     else console.log(res)
   });
  
}, 1001);
  
app.listen(3000, function(error ) {
    if(error) console.log(error)
    console.log("Server listening on PORT 3000")
});

The project structure will look like this: 
 

Run index.js file using below command: 

node index.js

Output: 

Before merge function
Server listening on PORT 3000
[
{ _id: 5ebb9129a99bde77b2efb809, name: ‘Gourav’, age: 10, __v: 0 },
{ _id: 5ebc3669a99bde77b2efb9ba, name: ‘Lalit’, age: 25, __v: 0 },
{ _id: 5ebc367da99bde77b2efb9bf, name: ‘Piyush’, age: 5, __v: 0 },
{ _id: 5ebd345f5d2d8a3534b2f391, name: ‘Manish’, age: 34, __v: 0 }
]
After merge function
[ { _id: 5ebb9129a99bde77b2efb809, name: ‘Gourav’, age: 10, __v: 0 }

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!