Mongoose Query.prototype.findOneAndReplace()方法

Mongoose Query.prototype.findOneAndReplace()方法

Mongoose Query API findOneAndReplace() 方法 用于使用MongoDB查询系统替换集合中的单个文档。它一次只能更新一个文档。

语法:

Query.prototype.findOneAndReplace(filter, replacement, options, callback)

参数: 它接受上述提到并描述的以下4个参数:

  • filter: 它是一个标识要替换的现有文档的mongoose对象。
  • replacement: 它是一个将替换现有文档的mongoose对象。
  • options: 它是一个可选的从Query.prototype.setOptions()派生出来的mongoose对象。
  • callback: 它是一个接受2个参数(错误和文档)的回调函数。

返回类型: 它以Query对象的形式返回响应。

创建Node应用并安装Mongoose:

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

mkdir folder_name
cd folder_name
npm init -y
touch main.js

步骤2: 完成Node.js应用程序后,使用以下命令安装所需模块:

npm install mongoose

示例1: 在这个示例中,我们将使用这种方法来替换一个名为“Luffy”的现有文档。

const mongoose = require('mongoose') 
  
// Database connection 
mongoose.connect('mongodb://localhost:27017/query-helpers', 
    { 
        dbName: 'event_db', 
        useNewUrlParser: true, 
        useUnifiedTopology: true
    }, err => err ? console.log(err) 
        : console.log('Connected to database')); 
  
const personSchema = new mongoose.Schema({ 
    name: { 
        type: String, 
    }, 
    age: { 
        type: Number, 
    } 
}); 
  
const personsArray = [ 
    { 
        name: 'Luffy', 
        age: 19 
    }, 
    { 
        name: 'Nami', 
        age: 20, 
    }, 
    { 
        name: 'Zoro', 
        age: 35 
    } 
] 
  
const Person = mongoose.model('Person', personSchema); 
  
(async () => { 
    await Person.insertMany(personsArray); 
    const res = await Person.findOneAndReplace( 
        { name: "Luffy" }, { name: "Usorp", age: 21 } 
    ); 
    console.log({ res }); 
})()

运行应用的步骤: 从项目的根目录中使用以下命令运行应用:

node main.js

输出:

Mongoose Query.prototype.findOneAndReplace()方法

使用MongoDB Compass的数据库的GUI表示:

Mongoose Query.prototype.findOneAndReplace()方法

示例2: 在这个示例中,我们将使用这个方法来替换一个年龄为”19″的文档.

const mongoose = require('mongoose') 
  
// Database connection 
mongoose.connect('mongodb://localhost:27017/query-helpers', 
    { 
        dbName: 'event_db', 
        useNewUrlParser: true, 
        useUnifiedTopology: true
    }, err => err ? console.log(err) 
        : console.log('Connected to database')); 
  
const personSchema = new mongoose.Schema({ 
    name: { 
        type: String, 
    }, 
    age: { 
        type: Number, 
    } 
}); 
  
const personsArray = [ 
    { 
        name: 'Luffy', 
        age: 19 
    }, 
    { 
        name: 'Nami', 
        age: 20, 
    }, 
    { 
        name: 'Zoro', 
        age: 35 
    } 
] 
  
const Person = mongoose.model('Person', personSchema); 
  
(async () => { 
    await Person.insertMany(personsArray); 
    const res = await Person.findOneAndReplace( 
        { age: 19 }, { name: "Usorp", age: 21 } 
    ); 
    console.log({ res }); 
})()

运行应用程序的步骤: 从项目的根目录中使用以下命令运行应用程序:

node main.js

输出:

Mongoose Query.prototype.findOneAndReplace()方法

使用MongoDB Compass的数据库的GUI表示:

Mongoose Query.prototype.findOneAndReplace()方法

参考: https://mongoosejs.com/docs/api/query.html#query_Query-findOneAndReplace

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程