0

Good morning, I have the following structure in 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"),
             }
        ]

I would like to receive the following answer, separating the same record but grouping by day:

"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"),
             },
        ]
    }

As the attendant adds all the attendance, I need to arrange a way to search for all missed appointments, but grouping by day

You can not get all, but do not group:

        {$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 Answers0