【问题标题】:How filter $lookup result如何过滤 $lookup 结果
【发布时间】:2019-05-15 00:21:18
【问题描述】:

我有一个使用$lookup 的查询,在此查询中我需要传递一个参数来“过滤”基于字段scheduleStart 的结果(我使用moment.js 将日期传递给我的查询)。我不知道我应该在哪里传递参数。

我的查询

   User.aggregate([{
          $match: {
            storeKey: req.body.store,     
          }
        },
        {
          $group: {
            _id: {
              id: "$_id",
              name: "$name",
              cpf: "$cpf",      
              phone: "$phone",
              email: "$email",
              birthday: "$birthday",
              lastName: "$lastname"      
            },
            totalServices: {
              $sum: "$services"
            },    
          }
        },
        {
          $lookup: {
            from: "schedules",
            "let": { "id": "$_id.phone" },
            "pipeline": [
               { "$match": { "$expr": { "$eq": ["$customer.phone", "$$id"] }}},
               { "$project": { "scheduleStart": 1, "scheduleEnd": 1, "value": 1 }}
            ],
            "as": "user_detail"
          }  
        },  
        {
          $project: {
            _id: 1,
            name: 1,
            name: 1,
            cpf: 1,      
            phone: 1,
            email: 1,
            birthday: 1,
            totalServices: 1,
            totalValue: { $sum : "$user_detail.value" },      
            count: {
              $sum: 1
            },
            user_detail: 1
          }
        },

我想在该查询中传递的参数:

 start = '2018-12-13 00:00'
 period = '2017-01-13 00:00'

我查询的实际结果:

6:
count: 1
totalServices: 0
totalValue: 73
user_detail: Array(2)
0: {_id: "5bb2832890c4f23d207b5d71", scheduleStart: "2018-10-02 08:20", scheduleEnd: "2018-10-02 08:40", value: 40}
1: {_id: "5bfd9c13e1193a4f30df05e4", scheduleStart: "2018-11-27 00:03", scheduleEnd: "2018-11-27 00:13", value: 33}

_id: {id: "5bfed8bd70de7a383855f09e", name: "Chris Santos G", phone: "11969109995", email: "csantosgrossi@gmail.com", birthday: "1992-03-06"}

如何传递参数以通过scheduleStart 过滤结果?我不知道我是否需要在 $lookup 内部或之后传递。

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework lookup


    【解决方案1】:

    您可以使用$match 阶段内的$lookup 管道内的过滤器使用$lte$gte 查询运算符

    { "$lookup": {
      "from": "schedules",
      "let": { "id": "$_id.phone" },
      "pipeline": [
        { "$match": {
          "$expr": { "$eq": ["$customer.phone", "$$id"] },
          "scheduleStart": {
            "$lte": moment("2018-12-13 00:00").toDate(),
            "$gte": moment("2017-01-13 00:00").toDate()
          }
        }},
        { "$project": { "scheduleStart": 1, "scheduleEnd": 1, "value": 1 }}
      ],
      "as": "user_detail"
    }}
    

    【讨论】:

    • 我正在使用ScheduleStart 的行来像这样scheduleStart: { '$gte': '2017-12-14 23:59', '$lte': '2018-12-14 00:00' } 进行测试并且不会工作
    • 因为这里的字段类型很重要。您已在架构中将其视为date,在这里您以string 传递。这就是我们在这里使用moment 的原因
    • 但在我的架构中,该字段也是一个字符串
    • @Matheus 抱歉回复晚了。但是如果是你的数据库中的一个字符串,并且你在这里将它作为一个字符串传递,那么它应该可以工作。可以看here
    • 我知道,你的回答是对的,但是这里还不行。
    猜你喜欢
    • 1970-01-01
    • 2020-01-31
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2020-04-07
    • 2013-10-06
    • 2018-07-30
    相关资源
    最近更新 更多