Mongoose API 的 Aggregate API.prototype.append() 方法用於執行聚合任務。它允許我們通過添加新操作來更新或修改當前管道。使用append()方法,我們可以將新的管道操作附加到當前的聚合管道。
用法:
aggregate(...).append( <Object/ArrayObject> )
參數:該方法接受單個參數,如下所述:
- operations:該方法將操作作為對象或對象數組形式的參數。
返回值:它以數組的形式返回聚合結果集。
設置 Node.js 應用程序:
步驟 1:使用以下命令創建 Node.js 應用程序:
npm init
步驟 2:創建 NodeJS 應用程序後,使用以下命令安裝所需的模塊:
npm install mongoose
項目結構: 項目結構將如下所示:
數據庫結構:數據庫結構如下所示,集合中存在以下文檔。
示例 1:在此示例中,我們使用 mongoose 建立了數據庫連接,並通過 cricketerSchema 定義了模型,具有三列或字段 “_id”、“name” 和 “nationality”。首先,我們定義了初始管道操作項目聚合,然後我們打印當前管道狀態。最後,我們使用附加了新的管道操作匹配聚合,最後使用 console.log 顯示附加操作之前和之後的管道狀態以及聚合結果。
文件名:app.js
Javascript
// Require mongoose module
const mongoose = require("mongoose");
// Set Up the Database connection
mongoose.connect(
"mongodb://localhost:27017/geeksforgeeks", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const cricketerSchema = new mongoose.Schema({
_id: Number,
name: String,
nationality: String
});
const Cricketer = mongoose.model(
'Cricketers', cricketerSchema);
const aggregate = Cricketer.aggregate();
aggregate.project({ name: 1, nationality: 1 })
const beforeAppend = aggregate.pipeline();
console.log("Before append - ", beforeAppend);
aggregate.append({ $match: { _id: 1 } })
.exec((error, success) => {
console.log("Result - ", success);
const afterAppend = aggregate.pipeline();
console.log("After append - ", afterAppend);
});
運行程序的步驟:要運行應用程序,請從項目的根目錄執行以下命令:
node app.js
輸出:
Before append - [ { '$project': { name: 1, nationality: 1 } } ] Result - [ { _id: 1, name: 'Virat Kohli', nationality: 'India' } ] After append - [ { '$project': { name: 1, nationality: 1 } }, { '$match': { _id: 1 } } ]
示例 2:在此示例中,不是將對象傳遞給append()方法中,我們傳遞包含管道操作的對象數組。
文件名:app.js
Javascript
// Require mongoose module
const mongoose = require("mongoose");
// Set Up the Database connection
mongoose.connect(
"mongodb://localhost:27017/geeksforgeeks", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const cricketerSchema = new mongoose.Schema({
_id: Number,
name: String,
nationality: String
});
const Cricketer = mongoose.model(
'Cricketers', cricketerSchema);
const aggregate = Cricketer.aggregate();
aggregate.project({ name: 1, nationality: 1 })
const beforeAppend = aggregate.pipeline();
console.log("Before append - ", beforeAppend);
const appendPipeline = [{ $match: { _id: 5 } }]
aggregate.append(appendPipeline).then((result) => {
console.log("Result - ", result);
const afterAppend = aggregate.pipeline();
console.log("After append - ", afterAppend);
});
運行程序的步驟:要運行應用程序,請從項目的根目錄執行以下命令:
node app.js
輸出:
Before append - [ { '$project': { name: 1, nationality: 1 } } ] Result - [ { _id: 5, name: 'Aaron Finch', nationality: 'Australia ' } ] After append - [ { '$project': { name: 1, nationality: 1 } }, { '$match': { _id: 5 } } ]
參考:https://mongoosejs.com/docs/api/aggregate.html#aggregate_Aggregate-append
相關用法
- Mongoose Aggregate.prototype.addFields()用法及代碼示例
- Mongoose Aggregate.prototype.catch()用法及代碼示例
- Mongoose Aggregate.prototype.exec()用法及代碼示例
- Mongoose Aggregate.prototype.model()用法及代碼示例
- Mongoose Aggregate.prototype.skip()用法及代碼示例
- Mongoose Aggregate.prototype.limit()用法及代碼示例
- Mongoose Aggregate.prototype.then()用法及代碼示例
- Mongoose Aggregate.prototype.sortByCount()用法及代碼示例
- Mongoose Aggregate.prototype.project()用法及代碼示例
- Mongoose Aggregate.prototype.pipeline()用法及代碼示例
- Mongoose Aggregate.prototype.match()用法及代碼示例
- Mongoose Aggregate.prototype.cursor()用法及代碼示例
- Mongoose Aggregate.prototype.sort()用法及代碼示例
- Mongoose Aggregate.prototype.lookup()用法及代碼示例
- Mongoose Aggregate prototype.unionWith()用法及代碼示例
- Mongoose Aggregate prototype.sample()用法及代碼示例
- Mongoose Aggregate prototype.unwind()用法及代碼示例
- Mongoose countDocuments()用法及代碼示例
- Mongoose deleteMany()用法及代碼示例
- Mongoose deleteOne()用法及代碼示例
- Mongoose estimatedDocumentCount()用法及代碼示例
- Mongoose exists()用法及代碼示例
- Mongoose find()用法及代碼示例
- Mongoose findById()用法及代碼示例
- Mongoose findByIdAndDelete()用法及代碼示例
注:本文由純淨天空篩選整理自kartikmukati大神的英文原創作品 Mongoose Aggregate.prototype.append() API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。