Mongoose Aggregate 原型。Mongoose API 的lookup() 方法用於執行聚合任務。它允許我們在同一數據庫中的集合之間執行左連接操作,以根據需求和條件過濾出文檔。讓我們通過一個例子來理解lookup()方法。
用法:
aggregate.lookup( <object> );
參數:此方法接受單個參數,如下所述:
- object:它用於指定方法執行的各種屬性,例如:from、localField、foreignField。
返回值:該方法以數組的形式返回聚合結果集。
設置 Node.js Mongoose 模塊:
步驟 1:使用以下命令創建 Node.js 應用程序:
npm init
步驟 2:創建 NodeJS 應用程序後,使用以下命令安裝所需的模塊:
npm install mongoose
項目結構: 項目結構將如下所示:
數據庫結構:數據庫結構如下所示,集合中存在以下文檔。
- 學生作品集:
- 標記集合:
示例 1:在此示例中,我們使用 mongoose 建立了數據庫連接,並通過 StudentSchema 和marksSchema 定義了模型。最後,我們通過以對象形式傳遞各種屬性值來調用loopup()方法。我們得到了數組形式的預期結果。為了獲得更好的圖像,我們從結果數組中提取第一個對象。
文件名: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 studentSchema = new mongoose.Schema({
name: { type: String },
age: { type: Number },
rollNumber: { type: Number },
});
const marksSchema = new mongoose.Schema({
english: Number,
maths: Number,
science: Number,
socialScience: Number,
hindi: Number,
rollNumber: Number
})
const Student = connectionObject.model('Student', studentSchema);
const Mark = connectionObject.model('Mark', marksSchema);
Student.aggregate().lookup({
from: 'marks',
localField: 'rollNumber', foreignField: 'rollNumber',
as: 'student_marks'
}).exec((error, result) => {
if (error) {
console.log('error - ', error);
} else {
console.log('result - ', result[0]);
}
})
運行程序的步驟:要運行應用程序,請從項目的根目錄執行以下命令:
node app.js
輸出:
result - { _id: new ObjectId("63a40a1065e8951038a391b1"), name: 'Student1', age: 20, rollNumber: 9, __v: 0, student_marks: [ { _id: new ObjectId("63a4aadedbfff3b8898083c3"), english: 50, maths: 60, science: 75, socialScience: 68, hindi: 58, rollNumber: 9, __v: 0 } ] }
示例 2:在此示例中,我們使用 mongoose 建立了數據庫連接,並通過 StudentSchema 和marksSchema 定義了模型。最後,我們使用聚合管道並配置 $lookup 對象。我們得到了數組形式的預期結果。最後,我們使用 forEach 方法迭代數組並顯示學生的姓名和學生的分數。
文件名: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 studentSchema = new mongoose.Schema({
name: { type: String },
age: { type: Number },
rollNumber: { type: Number },
});
const marksSchema = new mongoose.Schema({
english: Number,
maths: Number,
science: Number,
socialScience: Number,
hindi: Number,
rollNumber: Number
})
const Student = connectionObject.model('Student', studentSchema);
const Mark = connectionObject.model('Mark', marksSchema);
(async () => {
const result = await Student.aggregate([{ $lookup:
{ from: 'marks', localField: 'rollNumber',
foreignField: 'rollNumber', as: 'student_marks' } }])
result.forEach(student => {
console.log(student.name)
console.log(student.student_marks);
})
})();
運行程序的步驟:要運行應用程序,請從項目的根目錄執行以下命令:
node app.js
輸出:
Student1 [ { _id: new ObjectId("63a4aadedbfff3b8898083c3"), english: 50, maths: 60, science: 75, socialScience: 68, hindi: 58, rollNumber: 9, __v: 0 } ] Student3 [ { _id: new ObjectId("63a4aadedbfff3b8898083c4"), english: 90, maths: 60, science: 55, socialScience: 78, hindi: 68, rollNumber: 178, __v: 0 } ] Student4 [ { _id: new ObjectId("63a4aadedbfff3b8898083c5"), english: 55, maths: 68, science: 85, socialScience: 87, hindi: 85, rollNumber: 152, __v: 0 } ] Student2 [ { _id: new ObjectId("63a4aadedbfff3b8898083c6"), english: 75, maths: 88, science: 45, socialScience: 65, hindi: 80, rollNumber: 176, __v: 0 } ]
參考: https://mongoosejs.com/docs/api/aggregate.html#aggregate_Aggregate-lookup
相關用法
- Mongoose Aggregate.prototype.limit()用法及代碼示例
- Mongoose Aggregate.prototype.catch()用法及代碼示例
- Mongoose Aggregate.prototype.exec()用法及代碼示例
- Mongoose Aggregate.prototype.model()用法及代碼示例
- Mongoose Aggregate.prototype.skip()用法及代碼示例
- 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.cursor()用法及代碼示例
- Mongoose Aggregate.prototype.sort()用法及代碼示例
- Mongoose Aggregate.prototype.addFields()用法及代碼示例
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自sakshio0hoj大神的英文原創作品 Mongoose Aggregate.prototype.lookup() API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。