繁体   English   中英

聚合项目mongodb输出未预期

[英]aggregation project mongodb output not expected

我正在用JavaScript编写一个复杂的查询。 我正在使用聚合。 我正在使用两个表。 发票,旅行

这是我的代码。

      Invoice.aggregate([
        // filter the documents from invoice of only 2016
        {
          $match: {
            executed: {
              $gte: startDate,
              $lte: endDate
            },
            "modelHolder.name": 'Travel'
          }
        },

        // $lookup is working alone, it is not taking the input from function 1 of aggregate
        {
          $lookup: {
            from: "travels",
            localField: "modelHolder.id",
            foreignField: "_id",
            as: "dataFromTravels"
          }
        },

        // filter by date of reservation and date of arrival
          $match: { $or:
  [
    {
      'dataFromTravels.from.date': {
        $gte: departDate, $lte: endDate
      }
    },
    {
      'dataFromTravels.to.date': {
        $lte: arrivalDate
      },
    }
  ]
      }
    },


        {
          $limit: 2
        }
        // 2nd function to work on the first function output as the input
      ], function (err, result) {
        if (err) {
          return console.log(('ERROR', err));
          //next(err);
        } else {
          console.log('Result', result);
         // res.json(result);
          return;
        }
      });

      // console.log('invoice  !');
      // console.log(invoice._id);
      self.resume();
    })

    .on('error', function (err) {
      console.error('Error occurred while streaming invoices', err);
    })

    .on('end', function () {
      console.log('Successfully displayed invoices');
      cb();
    });

我想提供汽车列表,并计算在匹配功能限制的时间内我们使用每辆汽车的次数。

我添加了这个。

    {
$group: { "_id": "$dataFromTravels.car.plateNumber", "count": { $sum: 1 } }
            },

车在旅行台下面。 我刚买了3辆车 或者我在同一时期有数百辆汽车。

我该如何处理?

谢谢你的建议。

使用mongodb进行联接查询时。 目标集合存储为数组。 所以当你使用查找

{
    $lookup: {
      from: "travels",
      localField: "modelHolder.id",
      foreignField: "_id",
      as: "dataFromTravels"
   }
}

查询的输出将类似于:

{
  "_id": ObjectId("554dc5e937c1482c491d9d36"),
  // invoice data
  "dataFromTravels": [
    // travel data
  ]
}

使用mongodb聚合时,不能对嵌套文档使用$ match。 所以,在查询中要匹配

db.invoice.aggregate([
  {
    $match: {
      executed: {
        $gte: ISODate(startDate),
        $lte: ISODate(endDate)
      },
      "modelHolder.name": 'Travel'
    }
  },
  {
    $lookup: {
      from: "travels",
      localField: "modelHolder.id",
      foreignField: "_id",
      as: "dataFromTravels"
    }
  }, {
    $unwind: "$dataFromTravels"
  },{
  $match: {
    $or: [{
      'dataFromTravels.from.date': {
        $gte: ISODate(startDate),
        $lte: ISODate(endDate)
      }
    }, {
      'dataFromTravels.to.date': {
        $lte: ISODate(startDate)
      },
    }]
  }
  }, {
    $limit: 2
  }
]);

现在,dataFromTravels将作为对象提供,您可以在fromto属性上使用$ match

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM