MongoDB 聚合管道&关联处理

关联查询

汇率表数据

db.forex.insert([
    {
        ccy: "USD",
        rate: 6.91,
        date: new Date("2018-12-21")
    },
    {
        ccy: "GBP",
        rate: 8.72,
        date: new Date("2018-08-21")
    },
    {
        ccy: "CNY",
        rate: 1.0,
        date: new Date("2018-10-21")
    }
])

用户表数据

> db.accounts.find().pretty()
{
 "_id" : ObjectId("5d80c37349f3060f1212a055"),
 "name" : {
  "first_name" : "qingquan",
  "last_name" : "zeng"
 },
 "balance" : 100,
 "currency" : [
  "CNY",
  "USD"
 ]
}
{
 "_id" : ObjectId("5d80c37349f3060f1212a056"),
 "name" : {
  "first_name" : "fengxia",
  "last_name" : "yu"
 },
 "balance" : 200,
 "currency" : "GBP"
}

使用lookup来对表进行关联查询

  • from 要关联的表
  • localField 当前表字段
  • foreignField 当前表字段要关联的外表字段
  • as 新增字段展示关联的结果
db.accounts.aggregate([
    {
        $lookup: {
            from: "forex",
            localField: ‘currency‘,
            foreignField: "ccy",
            as: "forexData"
        }
    }
])

得出如下结果

{
 "_id" : ObjectId("5d80c37349f3060f1212a055"),
 "name" : {
  "first_name" : "qingquan",
  "last_name" : "zeng"
 },
 "balance" : 100,
 "currency" : [
  "CNY",
  "USD"
 ],
 "forexData" : [
  {
   "_id" : ObjectId("5d82226fc404a31cf5199942"),
   "ccy" : "USD",
   "rate" : 6.91,
   "date" : ISODate("2018-12-21T00:00:00Z")
  },
  {
   "_id" : ObjectId("5d82226fc404a31cf5199944"),
   "ccy" : "CNY",
   "rate" : 1,
   "date" : ISODate("2018-10-21T00:00:00Z")
  }
 ]
}
{
 "_id" : ObjectId("5d80c37349f3060f1212a056"),
 "name" : {
  "first_name" : "fengxia",
  "last_name" : "yu"
 },
 "balance" : 200,
 "currency" : "GBP",
 "forexData" : [
  {
   "_id" : ObjectId("5d82226fc404a31cf5199943"),
   "ccy" : "GBP",
   "rate" : 8.72,
   "date" : ISODate("2018-08-21T00:00:00Z")
  }
 ]
}

关联修改

将特定日期的外汇汇率写入银行账户文档

# 所有账号的 forexData 匹配的都是 2018-08-21 的记录
# 使用了 pipeline 之后,就不能同时使用 localField 与 foreignField 了
db.accounts.aggregate([
    {
        $lookup: {
            from: "forex",
            pipeline: [
                {
                    $match: {
                        date: new Date("2018-08-21")
                    }
                }
            ],
            as: "forexData"
        }
    }
])
{
 "_id" : ObjectId("5d80c37349f3060f1212a055"),
 "name" : {
  "first_name" : "qingquan",
  "last_name" : "zeng"
 },
 "balance" : 100,
 "currency" : [
  "CNY",
  "USD"
 ],
 "forexData" : [
  {
   "_id" : ObjectId("5d82226fc404a31cf5199943"),
   "ccy" : "GBP",
   "rate" : 8.72,
   "date" : ISODate("2018-08-21T00:00:00Z")
  }
 ]
}
{
 "_id" : ObjectId("5d80c37349f3060f1212a056"),
 "name" : {
  "first_name" : "fengxia",
  "last_name" : "yu"
 },
 "balance" : 200,
 "currency" : "GBP",
 "forexData" : [
  {
   "_id" : ObjectId("5d82226fc404a31cf5199943"),
   "ccy" : "GBP",
   "rate" : 8.72,
   "date" : ISODate("2018-08-21T00:00:00Z")
  }
 ]
}

将特定日期的外汇汇率写入余额大于100的银行账户文档

# 使用let来区分pipeline用到的字段为本表字段还是外表字段
# 要使用let声明的变量时,需要在pipeline中使用 $expr
db.accounts.aggregate([
    {
        $lookup: {
            from: "forex",
            let: {
                bal: "$balance"
            },
            pipeline: [
                {
                    $match: {
                        $expr: {
                            $and: [
                                {
                                    $eq: ["$date", new Date("2018-08-21")]
                                },
                                {
                                    $gt: ["$$bal", 100]
                                }
                            ]
                        }
                    }
                }
            ],
            as: "forexData"
        }
    }
])

MongoDB 聚合管道&关联处理

上一篇:一般问题处理记录(SqlServe)


下一篇:MySQL设置global变量和session变量的两种方法详解