根据数组元素日期时间和字段对文档进行汇总和排序

时间:2018-10-28 15:40:54

标签: node.js mongodb mongoose

当前,我具有以下架构:

User:{
     verified:boolean,
     history:[{
          type:string,
          dateTime:datetime
     }]
}

我需要根据history.type和history.datetime对数据进行汇总和排序。例如,我有100个文档。其中一半具有history.type =“ Testing”,并且每个历史记录都有自己的日期时间。 我需要匹配历史记录类型,并使用猫鼬nodejs对日期时间进行排序。

这是我所做的,但未按预期工作:

let user = await User.aggregate([
            {
                $match: {
                    "nameVerified":true,
                    'history.type' : 'NAME_VERIFIED'
                }
            },
            {$unwind: '$history'}, 
        {
            $match: {
                'history.type' : 'NAME_VERIFIED'
            }
        },  
        {
            $group : {
                _id :'$_id'
            }
          },
        {
            $sort: 
            {
                'history.dateTime': -1
            }
        }]);

样本数据:

{_id:1,verified:false,history:[...]}
{_id:2,verified:true,history:[{type:"NAME_VERIFIED",dateTime:2018-10-23},{type:"TEST",dateTime:2018-10-25}]}
{_id:3,verified:true,history:[{type:"NAME_VERIFIED",dateTime:2018-10-24},{type:"TEST",dateTime:2018-10-21}]}
{_id:4,verified:true,history:[{type:"NAME_VERIFIED",dateTime:2018-10-21},{type:"TEST",dateTime:2018-10-21}]}
{_id:5,verified:true,history:[{type:"NAME_VERIFIED",dateTime:2018-10-22},{type:"TEST",dateTime:2018-10-21}]}

预期结果:

{_id:3,verified:true,history:[{type:"NAME_VERIFIED",dateTime:2018-10-24},{type:"TEST",dateTime:2018-10-21}]}
{_id:2,verified:true,history:[{type:"NAME_VERIFIED",dateTime:2018-10-23},{type:"TEST",dateTime:2018-10-25}]}
{_id:5,verified:true,history:[{type:"NAME_VERIFIED",dateTime:2018-10-22},{type:"TEST",dateTime:2018-10-21}]}
{_id:4,verified:true,history:[{type:"NAME_VERIFIED",dateTime:2018-10-21},{type:"TEST",dateTime:2018-10-21}]}

有人可以提出解决方案吗?

2 个答案:

答案 0 :(得分:0)

以下查询将返回匹配history.type并按history.dateTime排序的文档数组。

User.aggregate([{
    $match: {
      "history.type": 'NAME_VERIFIED',
    }
  },
  {
    $unwind: '$history'
  },
  {
    $match: {
      "history.type": 'NAME_VERIFIED',
    }
  },
  {
    $sort: {
      'history.dateTime': -1
    }
  },
  {
    $group: {
      '_id': {
        '_id': '$_id',
        'verified': '$verified'
      },
      'history': {
        '$push': '$history'
      }
    }
  },
  {
    $project: {
      '_id': '$_id._id',
      'verified': '$_id.verified',
      'history': 1
    }
  }
])

如果应该更改标准中的任何内容,请告诉我,我将尝试重塑查询。

答案 1 :(得分:0)

最后,我找到了一种解决方案,如下所示:

User.aggregate([
        {
            $match: {
                "nameVerified":true,
                "history.type": 'NAME_VERIFIED',
            }
        },
        {
            $unwind: '$history'
        },
        {
            $match: {
                "nameVerified":true,
                "history.type": 'NAME_VERIFIED',
            }
        },
        {
            $sort: {
                'history.dateTime': -1
            }
        },
        {
            $group: {
                _id: '$_id',
                'history': {
                    '$push': '$history'
                }
            }
        },
        {
            $sort: {
                'history.dateTime': -1
            }
        },
        {
            $skip: skipNo
        },
        {
            $limit: limitNo
        }
        ])