Mongoose Aggregate.prototype.lookup()函数

Mongoose Aggregate.prototype.lookup()函数

Mongoose的Aggregate prototype.lookup() 方法是Mongoose API中用于执行聚合任务的方法。它允许我们在同一数据库的集合之间执行左连接操作,根据要求和条件筛选出文档。让我们通过一个示例来理解 lookup() 方法。

语法:

aggregate.lookup( <object> );

参数: 该方法接受一个参数,具体描述如下:

  • object: 用于指定各种属性,如:from、localField、foreignField,用于方法执行。

返回值: 该方法以数组的形式返回聚合结果集。

设置Node.js的Mongoose模块:

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

npm init

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

npm install mongoose

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

Mongoose Aggregate.prototype.lookup()函数

数据库结构: 数据库结构将会是这样的,以下文档存在于集合中。

  • 学生集合:

Mongoose Aggregate.prototype.lookup()函数

  • 标志收藏:

Mongoose Aggregate.prototype.lookup()函数

示例1: 在这个示例中,我们使用mongoose建立了一个数据库连接,并在studentSchema和marksSchema上定义了模型。最后,我们通过以对象形式传递各种属性值来调用lookup()方法。我们得到了期望的结果,它是一个数组的形式。为了获得更好的理解,我们从结果数组中提取了第一个对象。

文件名: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 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

// 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程