Mongoose Query.prototype.setUpdate()函数

Mongoose Query.prototype.setUpdate()函数

在Mongoose API中,Mongoose Query.prototype.setUpdate()方法用于Query对象。它允许我们使用新的值和表达式设置当前的更新操作。通过使用该方法,我们可以修改更新查询表达式。让我们通过一个示例来了解setUpdate()方法。

语法:

query.setUpdate( object );
JavaScript

参数: 此方法接受一个参数,如下所述:

  • object: 它用于以对象形式指定更新查询表达式。

返回值: 此方法返回undefined或不返回任何内容。

设置Node.js应用程序:

步骤1: 使用以下命令创建一个Node.js应用程序:

npm init
JavaScript

步骤2: 创建NodeJS应用程序后,使用以下命令安装所需的模块:

npm install mongoose
JavaScript

项目结构: 项目的结构将如下所示:

Mongoose Query.prototype.setUpdate()函数

数据库结构:

数据库结构将如下所示,此集合中包含以下文档。

Mongoose Query.prototype.setUpdate()函数

示例1:

在此示例中,我们展示了 setUpdate() 方法的功能。我们正在更新集合中一个文档的 orderNumber 字段。使用 setUpdate() 方法,我们可以更改查询表达式和更新值。

文件名:app.js

// 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.find({ name: "Bhavesh" }); 
query.update({}, { set: { orderNumber: 6 } }) 
query.setUpdate({set: { orderNumber: 0 } }) 
console.log(query.getUpdate()); 
query.exec((error, result) => { 
    if (error) { 
        console.log("Error -", error); 
    } else { 
        console.log("Result -", result); 
    } 
})
JavaScript

运行程序的步骤: 要运行应用程序,请从项目的根目录执行以下命令:

node app.js
JavaScript

输出:

{ '$set': { orderNumber: 0 } }
Result - {
  acknowledged: true,
  modifiedCount: 1,
  upsertedId: null,
upsertedCount: 0,
JavaScript

使用Robo3T GUI工具的数据库的GUI表示:

Mongoose Query.prototype.setUpdate()函数

示例2: 在这个示例中,我们展示了setUpdate()方法的功能。我们在集合中的一个文档中更新了orderNumber字段。使用setUpdate()方法改变了我们使用update()方法更新的字段。最后,我们只更新了一个字段,即orderNumber。

文件名:app.js

// 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.find({ name: "Aditya" }); 
query.update({}, { set: { name: "Aaditya" } }) 
query.setUpdate({set: { orderNumber: 20 } }) 
console.log(query.getUpdate()); 
query.then((result) => { 
    console.log("Result -", result); 
}).catch((err) => { 
    console.log(err); 
})
JavaScript

运行程序的步骤: 要运行应用程序,请从项目的根目录执行以下命令:

node app.js
JavaScript

输出:

{ '$set': { orderNumber: 20 } }

Result - {
  acknowledged: true,
  modifiedCount: 1,  
  upsertedId: null,  
  upsertedCount: 0,  
  matchedCount: 1    
}
JavaScript

使用Robo3T GUI工具的数据库的GUI表示:

Mongoose Query.prototype.setUpdate()函数

参考文献: https://mongoosejs.com/docs/api/query.html#query_Query-setUpdate

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册