过滤嵌套数组mongodb组日

时间:2018-05-21 12:12:54

标签: mongodb

早上好,我在mongo中有以下结构:

"company" : "ESPIRAL",
"osServico" : [ 
        {
        "_id" : ObjectId("5afdbb931e511b23b621ff93"),
        "atend" : [ 
            {
                "aut" : null,
                "_id" : ObjectId("5afdc1369d845d25c5196044"),
                "tec" : ObjectId("5af18fa5a0d18a0320149de4"),
                "data" : ISODate("2018-05-16T00:00:00.000Z"),}, 
            {
                "aut" : null,
                "_id" : ObjectId("5afdc74a58f31a284d8d4d4f"),
                "tec" : ObjectId("5af18fa5a0d18a0320149de4"),
                "data" : ISODate("2018-05-16T00:00:00.000Z"),
            },
            {
            "aut" : null,
                "_id" : ObjectId("5afdc7b577ee6b28948137a8"),
                "tec" : ObjectId("5ad9fdea68ab9f0014ddb5d2"),
                "data" : ISODate("2018-05-15T00:00:00.000Z"),
             }
        ]

我希望收到以下答案,将相同的记录分开但按天分组:

"company" : "ESPIRAL",
"osServico" : [ 
        {
        "_id" : ObjectId("5afdbb931e511b23b621ff93"),
        "atend" : [
            {
                " aut " : null,
                "_id" : ObjectId("5afdc7b577ee6b28948137a8"),
                "tec" : ObjectId("5ad9fdea68ab9f0014ddb5d2"),
                "data" : ISODate("2018-05-15T00:00:00.000Z"),
             }
        ]
    }

"company" : "ESPIRAL",
"osServico" : [ 
        {
        "_id" : ObjectId("5afdbb931e511b23b621ff93"),
        "atend" : [ 
            {
                " aut " : null,
                "_id" : ObjectId("5afdc1369d845d25c5196044"),
                "tec" : ObjectId("5af18fa5a0d18a0320149de4"),
                "data" : ISODate("2018-05-16T00:00:00.000Z"),}, 
            {
                "aut" : null,
                "_id" : ObjectId("5afdc74a58f31a284d8d4d4f"),
                "tec" : ObjectId("5af18fa5a0d18a0320149de4"),
                "data" : ISODate("2018-05-16T00:00:00.000Z"),
             },
        ]
    }

随着服务员增加所有出席情况,我需要安排一种方法来搜索所有错过的约会,但是按天分组

你无法得到所有,但不要分组:

        {$match: {"osServico.atend.data": {$lt: data}}},
        {$addFields: {
            "osServico": {
                "$map": {
                    "input": "$osServico",
                    "as": "os",
                    "in": {
                        "_id": "$$os._id",
                        "servico": "$$os.servico",
                        "atend": {
                            "$filter": {
                                "input": "$$os.atend",
                                "as": "atend",
                                "cond": {
                                    $and : [ 
                                        { $lt: [ "$$atend.data", data] }
                                    ]
                                }
                            }
                        }
                    }
                }
            }

0 个答案:

没有答案