Mongoose API 的 Mongoose Query API.prototype.w() 方法用于查询对象。它允许我们设置必须确认写入操作的指定数量的MongoDB服务器。在写入事件成功执行之前。将“1”作为参数提供给该方法表示由一台服务器确认,将“majority”作为参数提供给该方法表示由该服务器的多个副本集确认。让我们通过一个例子来理解w()方法。
我们可以通过以下方法访问此选项或w()方法:
- deleteOne()
- deleteMany()
- findOneAndDelete()
- findOneAndReplace()
- findOneAndUpdate()
- remove()
- update()
- updateOne()
- updateMany()
用法:
query.deleteOne( ... ).w( val );
参数:该方法接受单个参数,如下所述:
- val:用于指定数字或字符串,1表示从1个MongoDB服务器获取确认,“majority”从大多数副本集获取确认。
返回值:该方法返回查询对象。
设置 Node.js 应用程序:
步骤 1:使用以下命令创建 Node.js 应用程序:
npm init
步骤 2:创建 NodeJS 应用程序后,使用以下命令安装所需的模块:
npm install mongoose
项目结构: 项目结构将如下所示:

数据库结构:数据库结构如下所示,集合中存在以下文档。

示例 1:在此示例中,我们将说明以下函数w()方法通过访问它updateOne()方法。
文件名:app.js
Javascript
// Require mongoose module
const mongoose = require("mongoose");
// Set Up the Database connection
const URI = "mongodb://localhost:27017/geeksforgeeks"
const connectionObject = mongoose.createConnection(URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const Customer = connectionObject.model(
'Customer', new mongoose.Schema({
name: String,
address: String,
orderNumber: Number,
}));
(async () => {
const query = Customer.updateOne(
{ name: "Chintu" }, { orderNumber: 10 }
);
query.w(1);
query.exec((error, result) => {
if (error) {
console.log('Error -', error);
} else {
console.log('Result -', result);
}
})
})();
运行程序的步骤:要运行应用程序,请从项目的根目录执行以下命令:
node app.js
输出:
Result - { acknowledged: true, modifiedCount: 1, upsertedId: null, upsertedCount: 0, matchedCount: 1 }
使用 Robo3T GUI 工具的数据库的 GUI 表示:

示例 2:在此示例中,我们将说明以下函数w()方法通过访问它deleteOne()方法。最后,我们从集合中删除一份文档,寻求大多数副本集的确认。
文件名:app.js
Javascript
// Require mongoose module
const mongoose = require("mongoose");
// Set Up the Database connection
const URI = "mongodb://localhost:27017/geeksforgeeks";
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.deleteOne({ name: "Bhavesh" });
query.w("majority");
query.then(result => {
console.log(result);
}).catch(error => {
console.log(error);
})
运行程序的步骤:要运行应用程序,请从项目的根目录执行以下命令:
node app.js
输出:
{ acknowledged: true, deletedCount: 1 }
使用 Robo3T GUI 工具的数据库的 GUI 表示:

参考:https://mongoosejs.com/docs/api/query.html#query_Query-w
相关用法
- Mongoose Query.prototype.all()用法及代码示例
- Mongoose Query.prototype.and()用法及代码示例
- Mongoose Query.prototype.batchSize()用法及代码示例
- Mongoose Query.prototype.box()用法及代码示例
- Mongoose Query.prototype.catch()用法及代码示例
- Mongoose Query.prototype.centerSphere()用法及代码示例
- Mongoose Query.prototype.circle()用法及代码示例
- Mongoose Query.prototype.collation()用法及代码示例
- Mongoose Query.prototype.cursor()用法及代码示例
- Mongoose Query.prototype.distinct()用法及代码示例
- Mongoose Query.prototype.exec()用法及代码示例
- Mongoose Query.prototype.explain()用法及代码示例
- Mongoose Query.prototype.geometry()用法及代码示例
- Mongoose Query.prototype.getOptions()用法及代码示例
- Mongoose Query.prototype.getPopulatedPaths()用法及代码示例
- Mongoose Query.prototype.getUpdate()用法及代码示例
- Mongoose Query.prototype.gt()用法及代码示例
- Mongoose Query.prototype.gte()用法及代码示例
- Mongoose Query.prototype.hint()用法及代码示例
- Mongoose Query.prototype.lean()用法及代码示例
- Mongoose Query.prototype.limit()用法及代码示例
- Mongoose Query.prototype.lt()用法及代码示例
- Mongoose Query.prototype.lte()用法及代码示例
- Mongoose Query.prototype.map()用法及代码示例
- Mongoose Query.prototype.maxTimeMS()用法及代码示例
注:本文由纯净天空筛选整理自kartikmukati大神的英文原创作品 Mongoose Query.prototype.w() API。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。