Query.prototype.merge() 如何在Mongoose中工作?
Query.prototype.merge()函数用于将另一个 Query 或条件对象合并到使用此函数的当前查询中,即此查询。当查询通过时,条件、字段选择和选项被合并。
句法:
Query.prototype.merge()
参数:该函数有一个对象/查询类型的源参数。
返回值:此函数返回查询对象。
Mongoose安装:
npm install mongoose
安装mongoose模块后,您可以使用命令在命令提示符下检查您的mongoose版本。
npm mongoose --version
之后,您可以创建一个文件夹并添加一个文件,例如 index.js,如下所示。
数据库:这里使用的示例数据库如下所示:
示例 1:
index.js
const mongoose = require('mongoose');
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
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);
index.js
const express = require('express');
const mongoose = require('mongoose');
const app = express()
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
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")
});
项目结构将如下所示:
使用以下命令运行index.js文件:
node index.js
输出:
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 } ]
示例 2:
index.js
const express = require('express');
const mongoose = require('mongoose');
const app = express()
// Database connection
mongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {
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")
});
项目结构将如下所示:
使用以下命令运行index.js文件:
node index.js
输出:
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 }
参考: https ://mongoosejs.com/docs/api/query.html#query_Query-merge