Mongo聚合管道在2个运算符后停止过滤。使用meteorhacks聚合

时间:2016-01-07 01:33:37

标签: mongodb meteor aggregation-framework

我正在使用Meteor与meteorhacks:聚合包。我想拉出今天的约会生日。

var today = new Date();

  result = ContactList.aggregate(
    {
      $project: {
        name: 1,
        birthday:1,
        todayDayOfYear:{"$dayOfYear":today},
        dayOfYear:{"$dayOfYear":"$birthday"},

      }
    }, 
    {
      $project: {
        name:1,
        birthday:1,
        dayDifference:{"$subtract":["$dayOfYear","$todayDayOfYear"]}
      }
    },
    {
      $match:{
        dayDifference:0
      }
    }

  );

  console.log(result);

聚合似乎在第二个$ project之后停止。即使我使用了$ match,代码也会输出所有条目。

I20160107-09:19:04.314(8)? [ { _id: 'e4cK95xh453kamAw2',
I20160107-09:19:04.315(8)?     name: 'Today',
I20160107-09:19:04.315(8)?     birthday: Fri Jan 01 2016 00:00:00        GMT+0800 (SGT),
I20160107-09:19:04.315(8)?     dayDifference: 358 },
I20160107-09:19:04.315(8)?   { _id: 'z93CfgwbHvHPurYxe',
I20160107-09:19:04.315(8)?     name: 'hello',
I20160107-09:19:04.315(8)?     birthday: Sat Jan 02 2016 00:00:00      GMT+0800 (SGT),
I20160107-09:19:04.316(8)?     dayDifference: -6 },
I20160107-09:19:04.316(8)?   { _id: 'YkrjyFNp6Ei28qrrH',
I20160107-09:19:04.316(8)?     name: 'yoyo',
I20160107-09:19:04.316(8)?     birthday: Sun Jan 03 2016 00:00:00 GMT+0800 (SGT),
I20160107-09:19:04.316(8)?     dayDifference: -5 },
I20160107-09:19:04.316(8)?   { _id: 'i2idScCcsKjimiPYW',
I20160107-09:19:04.317(8)?     name: 'yolo',
I20160107-09:19:04.317(8)?     birthday: Mon Jan 04 2016 00:00:00 GMT+0800 (SGT),
I20160107-09:19:04.317(8)?     dayDifference: -4 },
I20160107-09:19:04.317(8)?   { _id: 'mSBHiFigA8h7egs6J',
I20160107-09:19:04.317(8)?     name: 'yesterday',
I20160107-09:19:04.318(8)?     birthday: Tue Jan 05 2016 00:00:00 GMT+0800 (SGT),
I20160107-09:19:04.318(8)?     dayDifference: -3 },
I20160107-09:19:04.318(8)?   { _id: 'CnZqpnd9d6K66R8GL',
I20160107-09:19:04.318(8)?     name: 'today',
I20160107-09:19:04.318(8)?     birthday: Wed Jan 06 2016 00:00:00 GMT+0800 (SGT),
I20160107-09:19:04.319(8)?     dayDifference: -2 },
I20160107-09:19:04.319(8)?   { _id: 'RxuczFihgEjQQtA2E',
I20160107-09:19:04.319(8)?     name: 'tomorrow',
I20160107-09:19:04.319(8)?     birthday: Thu Jan 07 2016 00:00:00 GMT+0800 (SGT),
I20160107-09:19:04.319(8)?     dayDifference: -1 },
I20160107-09:19:04.319(8)?   { _id: 'EBgdyPSpXDkgBd7Y7',
I20160107-09:19:04.320(8)?     name: 'day after tomorrow',
I20160107-09:19:04.320(8)?     birthday: Fri Jan 08 2016 00:00:00 GMT+0800 (SGT),
I20160107-09:19:04.320(8)?     dayDifference: 0 } ]

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

请改为尝试:

today: function() {
   Contactlist.find({"birthday":{ $gt: new Date()}});
}