首页 > 解决方案 > 带有一些字段的 mongodb 组并全部推回

问题描述

我有以下收藏

_id:60b8c9294af8f15198249e5b
title:"abcd"
note:"abcdfregh"
date:2021-06-03T12:16:28.598+00:00
User_id:2
createdAt:2021-06-03T12:20:57.261+00:00
updatedAt:2021-06-03T12:20:57.261+00:00
Journal_id:15
__v:0

我正在尝试在日期中对月份和年份进行 $group 并 $push 所有字段

询问:

Upload.aggregate([
        { $match : { User_id : Number(user_id)}},
        { $group : { 
            _id : { month: { $month : "$date" },year: {$year : "$date"}},
        details : {$push : {"title" :"$title","note" : "$note"}}
    }},
            ]),

但我得到这样的输出:

[
  { _id: 'abcd', details: [ [Object], [Object] ] },
  { _id: 'aabghk\tinjcwmdsoi', details: [ [Object] ] }
]

我是 mongodb 的新手,正在努力理解这个错误;

我需要的期望输出是:

预期输出:

[
  { _id: 'abcd', details: [ {title:"title here", note: "note here"}, {title:"title here", note: "note here"} ] },
  { _id: 'aabghk\tinjcwmdsoi', details: [   {title:"title here", note: "note here"} ] }
]

标签: node.jsmongooseaggregation-framework

解决方案


推荐阅读