Mongoose 聚合操作的prototype.unionWith()函数

Mongoose 聚合操作的prototype.unionWith()函数

聚合操作的Aggregate API.prototype.unionWith() 方法用于执行聚合任务。 它允许我们将两个集合的管道结果组合成一个单一的结果集。然而,它也允许结果集中存在重复值,但是顺序没有指定。

语法:

aggregate.unionWith( options )

参数: 此方法接受一个参数,如上所述,具体讨论如下:

  • options: 它接受一个包含两个参数的对象,其中包括你想要执行联合操作的集合名称和管道数组。

返回值: 此方法以数组形式返回一个合并的结果集。

设置Node.js应用程序:

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

npm init

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

npm install mongoose

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

Mongoose 聚合操作的prototype.unionWith()函数

数据库结构: 数据库的结构将如下所示,以下文档存在于集合中。

Mongoose 聚合操作的prototype.unionWith()函数

示例1: 在这个示例中,我们使用mongoose建立了一个数据库连接,并定义了一个cricketerSchema的模型 ,其中有三列或字段“_id”、“name”“nationality”。我们还定义了另一个模式iplPlayerSchema, 其中有四列或字段“_id”、“name”、“teamName”“nationality”。 最后,我们使用 aggregate() 方法在Cricketer模型上选择“nationality”和“_id”字段,并将其存储在aggregate变量中。 在这个aggregate变量上,我们访问了unionWith()方法,它的参数中我们发送了另一个集合的名称来执行union操作并获取一个合并的结果集。

文件名:app.js

// 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 iplPlayerSchema = new mongoose.Schema({ 
    _id: Number, 
    name: String, 
    teamName: String, 
    nationality: String 
}); 
  
const Cricketer = mongoose.model('Cricketers', cricketerSchema); 
const IplPlayer = mongoose.model('IplPlayers', iplPlayerSchema); 
  
const aggregate = Cricketer.aggregate( 
    [{ project: { nationality: 1, _id: 0 } }] 
) 
  
aggregate.unionWith({ 
    coll: "iplplayers", 
    pipeline: [{project: { _id: 0, nationality: 1 } }] 
}).then(result => { 
    console.log(result) 
}).catch(err => { 
    console.log(err) 
})

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

node app.js

输出:

[
  { nationality: 'India' },       
  { nationality: 'Australia' },   
  { nationality: 'England ' },    
  { nationality: 'India' },       
  { nationality: 'South Africa' },
  { nationality: 'Bangladesh' }   
]

示例2: 在这个示例中,我们将从两个集合中获取所有字段,并将它们合并到一个结果集中。要实现这个目标,我们只需要向 unionWith() 方法提供“coll”值。

文件名:app.js

// 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 iplPlayerSchema = new mongoose.Schema({ 
    _id: Number, 
    name: String, 
    teamName: String, 
    nationality: String 
}); 
  
const Cricketer = mongoose.model('Cricketers', cricketerSchema); 
const IplPlayer = mongoose.model('IplPlayers', iplPlayerSchema); 
  
const aggregate = Cricketer.aggregate() 
  
aggregate.unionWith({ coll: "iplplayers" }) 
    .exec((err, result) => { 
        if (err) { 
            console.log(err) 
        } else { 
            console.log(result) 
        } 
    })

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

node app.js

输出:

[
  { 
      _id: 1, name: 'Virat Kohli', 
      nationality: 'India', 
      __v: 0 
  },     
  { 
      _id: 2, 
      name: 'David Warner', 
      nationality: 'Australia', 
      __v: 0 
  },
  { 
      _id: 3, 
      name: 'Ben Stokes', 
      nationality: 'England ', 
      __v: 0 
  },   
  {
    _id: 1,
    name: 'Rohit Sharma',
    teamName: 'Mumbai Indians',
    nationality: 'India',
    __v: 0
  },
  {
    _id: 2,
    name: 'David Miller',
    teamName: 'Chennai Super Kings',
    nationality: 'South Africa',
    __v: 0
  },
  {
    _id: 3,
    name: 'Shakib Al Hasan',
    teamName: 'Kolkata Knight Riders',
    nationality: 'Bangladesh',
    __v: 0
  }
]

参考: https://mongoosejs.com/docs/api/aggregate.html#aggregate_Aggregate-unionWith

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程