如何在节点js中使用mongoose将mongodb中的数据分组?

时间:2019-09-13 14:59:44

标签: javascript node.js mongodb mongoose mongodb-query

我的mongoDB中有一个这样的集合,其中包含每个日期的出勤数据

[
    {
        "_id": {
            "$oid": "5d7ba83eb3e51d00bd86d3d5"
        },
        "empid": "a1",
        "date": {
            "$date": "2019-07-01T00:00:00.000Z"
        },
        "attendence": "P",
        "name": "ravi",
        "id": "a1e428379285f56c021f4792e74ed520",
        "__v": 0
    },
    {
        "_id": {
            "$oid": "5d7ba87390632f00e9cdf4ea"
        },
        "empid": "a1",
        "date": {
            "$date": "2019-07-02T00:00:00.000Z"
        },
        "attendence": "P",
        "name": "ravi",
        "id": "75f8f4bd1db70519d90e740a89d5b77d",
        "__v": 0
    },{
        "_id": {
            "$oid": "5d7ba889f88e710103901300"
        },
        "empid": "a1",
        "date": {
            "$date": "2019-07-03T00:00:00.000Z"
        },
        "attendence": "A",
        "name": "ravi",
        "id": "e85904145b5c104f108a701ab4a7d511",
        "__v": 0
    },
    {
        "_id": {
            "$oid": "5d7ba8eac7ad01014124d8cb"
        },
        "empid": "a2",
        "date": {
            "$date": "2019-07-01T00:00:00.000Z"
        },
        "attendence": "A",
        "name": "rajat",
        "id": "c91a157c038328d0a9a9d501b2322ee4",
        "__v": 0
    },
    {
        "_id": {
            "$oid": "5d7ba8f4d20e630155617797"
        },
        "empid": "a2",
        "date": {
            "$date": "2019-07-02T00:00:00.000Z"
        },
        "attendence": "P",
        "name": "rajat",
        "id": "ec8d2107f1ec9c509845232f5aeeadd4",
        "__v": 0
    }
]

。我想aggregate or group data,以便检查哪个用户不在那个日期。

filter上的预期输出

[
    {

        "empid": "a1",
        "name": "ravi",
        data :[
            {
                "_id": {
                    "$oid": "5d7ba83eb3e51d00bd86d3d5"
                },
                "date": {
                    "$date": "2019-07-01T00:00:00.000Z"
                },
                "id": "a1e428379285f56c021f4792e74ed520",
                "attendence": "P",
            } ,
            {
                "_id": {
                    "$oid": "5d7ba87390632f00e9cdf4ea"
                },
                "date": {
                    "$date": "2019-07-02T00:00:00.000Z"
                },
                "attendence": "P",
                "id": "75f8f4bd1db70519d90e740a89d5b77d",
            },
            {
                "_id": {
                    "$oid": "5d7ba889f88e710103901300"
                },
                "date": {
                    "$date": "2019-07-03T00:00:00.000Z"
                },
                "attendence": "A",
                "id": "e85904145b5c104f108a701ab4a7d511",
            }
        ]
    },{
        "empid": "a2",
        "name": "rajat",
        data:[
            {
                "_id": {
                    "$oid": "5d7ba8eac7ad01014124d8cb"
                },
                "id": "c91a157c038328d0a9a9d501b2322ee4",
                "attendence": "A",
                "date": {
                    "$date": "2019-07-01T00:00:00.000Z"
                },
            },
            {
                "_id": {
                    "$oid": "5d7ba8f4d20e630155617797"
                },
                "id": "ec8d2107f1ec9c509845232f5aeeadd4",
                "attendence": "P",
                "date": {
                    "$date": "2019-07-02T00:00:00.000Z"
                },
            }
        ]
    }

] 

这是我的代码 https://codesandbox.io/s/lively-tree-hd0fo

app.get("/filter", async (req, res) => {
  try {
    // console.log(data);
    var blog = BlogPostModel();
    console.log("before save");
    let filterBlog = await BlogPostModel.find({
      date: { $gte: "2019-Jul-01", $lte: "2019-Jul-31" }
    });
    //when fail its goes to catch
    console.log(filterBlog); //when success it print.
    res.send(filterBlog);
  } catch (error) {
    console.log(error);
  }
});

当前,我正在获取2019-Jul-012019-Jul-31之间的所有数据

但是我需要aggregate或组数据,在mongoos中可以吗?

有任何更新吗?

0 个答案:

没有答案