Mongoose API 的 Aggregate API.prototype.cursor() 方法用於執行聚合任務。它允許我們迭代結果集。聚合遊標對象允許我們對結果集中的每個文檔對象一一執行迭代任務。
用法:
aggregate(...).cursor( options )
Parameters: 該方法接受單個參數,如下所述:
- options: 它是一個用於設置方法參數的對象。這個對象有一個批量大小可用於設置光標批量大小的參數。
返回值:它返回表示當前聚合管道操作的遊標對象。
設置 Node.js 應用程序:
步驟 1:使用以下命令創建 Node.js 應用程序:
npm init
步驟 2:創建 NodeJS 應用程序後,使用以下命令安裝所需的模塊:
npm install mongoose
項目結構: 項目結構將如下所示:

數據庫結構:數據庫結構如下所示,集合中存在以下文檔。

示例 1:在此示例中,我們使用 mongoose 建立了數據庫連接,並通過 cricketerSchema 定義了模型,具有三列或字段 “_id”、“name” 和 “nationality”。最後,我們呼籲cursor()聚合對象上的方法到遊標的引用。為了迭代聚合管道返回的所有文檔對象,我們使用eachAsyn()方法上光標對象。
文件名: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 cursorReference = Cricketer
.aggregate([{ $project: { name: 1 } }])
.cursor({ batchSize: 1000 });
cursorReference.eachAsync((document, count) => {
console.log(document, count);
})
運行程序的步驟:要運行應用程序,請從項目的根目錄執行以下命令:
node app.js
輸出:
{ _id: 3, name: 'Ben Stokes' } 0 { _id: 2, name: 'David Warner' } 1 { _id: 5, name: 'Aaron Finch' } 2 { _id: 7, name: 'K L Rahul' } 3 { _id: 6, name: 'Hardik Pandya' } 4 { _id: 1, name: 'Virat Kohli' } 5 { _id: 4, name: 'Rohit Sharma' } 6
示例 2:在此示例中,我們使用 mongoose 建立了數據庫連接,並通過 cricketerSchema 定義了模型,具有三列或字段 “_id”、“name” 和 “nationality”。最後,為了獲取我們正在使用的當前聚合管道的第一個文檔next()遊標對象上的方法,該方法返回當前管道操作的第一個文檔。
文件名: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);
Cricketer
.aggregate([{ $project: { name: 1 } }])
.cursor({ batchSize: 1000 })
.next()
.then((firstDocument) => {
console.log(firstDocument)
});
運行程序的步驟:要運行應用程序,請從項目的根目錄執行以下命令:
node app.js
輸出:
{ _id: 3, name: 'Ben Stokes' }
參考:https://mongoosejs.com/docs/api/aggregate.html#aggregate_Aggregate-cursor
相關用法
- 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.append()用法及代碼示例
- Mongoose Aggregate.prototype.sortByCount()用法及代碼示例
- Mongoose Aggregate.prototype.project()用法及代碼示例
- Mongoose Aggregate.prototype.pipeline()用法及代碼示例
- Mongoose Aggregate.prototype.match()用法及代碼示例
- Mongoose Aggregate.prototype.sort()用法及代碼示例
- Mongoose Aggregate.prototype.addFields()用法及代碼示例
- 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.cursor() API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。