erickles
erickles

Reputation: 157

$map upto three nested array mongodb aggregation

I´m facing a challenge here. I have this collection here

    {
        "_id" : ObjectId("5e0ff6d424f9fc12bc3d9464"),
        "name" : "Pizzaria Don Juan",
        "active" : true,
        "branches" : [
                {
                        "location" : {
                                "type" : "Point",
                                "coordinates" : [ ]
                        },
                        "_id" : ObjectId("5e19cafc31d60216b8dbd649"),
                        "name" : "Parque da Mooca",
                        "address" : "Rua Dianópolis",
                        "addressNumber" : 1283,
                        "federalId" : "10.445.089/0001-44",
                        "complement" : "Ap 55",
                        "postalCode" : "03126-007",
                        "coveredArea" : 0,
                        "neighborhood" : "Parque da Mooca",
                        "deliveryTime" : 0,
                        "deliveryRate" : 0,
                        "standard" : false,
                        "city" : "Mococa",
                        "state" : "RJ",
                        "emails" : [ ],
                        "phones" : [ ],
                        "daysWeek" : [ ],
                        "socialMedias" : [ ],
                        "paymentTerms" : [ ],
                        "sections" : [ ]
                },
                {
                        "location" : {
                                "type" : "Point",
                                "coordinates" : [ ]
                        },
                        "_id" : ObjectId("5e19c9a531d60216b8dbd639"),
                        "name" : "Principal",
                        "address" : "Rua Nicolau Filizola",
                        "addressNumber" : null,
                        "federalId" : "10.445.089/0001-53",
                        "complement" : "",
                        "postalCode" : "05547-010",
                        "coveredArea" : 0,
                        "neighborhood" : "Jardim Rosa Maria",
                        "deliveryTime" : 0,
                        "deliveryRate" : 0,
                        "standard" : true,
                        "city" : "São Paulo",
                        "state" : "SP",
                        "emails" : [
                                {
                                        "_id" : ObjectId("5e19ca9531d60216b8dbd643"),
                                        "name" : "Contato",
                                        "address" : "[email protected]"
                                },
                                {
                                        "_id" : ObjectId("5e19ca9531d60216b8dbd642"),
                                        "name" : "Contato2",
                                        "address" : "[email protected]"
                                }
                        ],
                        "phones" : [
                                {
                                        "_id" : ObjectId("5e19ca9531d60216b8dbd645"),
                                        "name" : "Principal",
                                        "number" : "(11) 99740-2216"
                                },
                                {
                                        "_id" : ObjectId("5e19ca9531d60216b8dbd644"),
                                        "name" : "Secundario",
                                        "number" : "(11) 2562-2759"
                                }
                        ],
                        "daysWeek" : [
                                {
                                        "_id" : ObjectId("5e1cf99741c52d4a587a9162"),
                                        "startsAt" : 64800000,
                                        "endsAt" : 82800000,
                                        "opens" : true,
                                        "dayWeekId" : ObjectId("5e1a124a17fd054900a1afb2")
                                },
                                {
                                        "_id" : ObjectId("5e1cf99741c52d4a587a9161"),
                                        "startsAt" : 0,
                                        "endsAt" : 0,
                                        "opens" : false,
                                        "dayWeekId" : ObjectId("5e1a126817fd054900a1afb3")
                                },
                                {
                                        "_id" : ObjectId("5e1cfbed41c52d4a587a9170"),
                                        "startsAt" : 64980000,
                                        "endsAt" : 82800000,
                                        "opens" : true,
                                        "dayWeekId" : ObjectId("5e1a126e17fd054900a1afb4")
                                },
                                {
                                        "_id" : ObjectId("5e1b8fac96516432845e364c"),
                                        "startsAt" : 64980000,
                                        "endsAt" : 82800000,
                                        "opens" : true,
                                        "dayWeekId" : ObjectId("5e1a127517fd054900a1afb5")
                                },
                                {
                                        "_id" : ObjectId("5e1cfbed41c52d4a587a916f"),
                                        "startsAt" : 64980000,
                                        "endsAt" : 82800000,
                                        "opens" : true,
                                        "dayWeekId" : ObjectId("5e1a127a17fd054900a1afb6")
                                },
                                {
                                        "_id" : ObjectId("5e1cfbed41c52d4a587a916e"),
                                        "startsAt" : 64800000,
                                        "endsAt" : 82800000,
                                        "opens" : true,
                                        "dayWeekId" : ObjectId("5e1a23f8bf353f493c74e8ae")
                                },
                                {
                                        "_id" : ObjectId("5e1cfbed41c52d4a587a916d"),
                                        "startsAt" : 61380000,
                                        "endsAt" : 83154000,
                                        "opens" : true,
                                        "dayWeekId" : ObjectId("5e1a2407bf353f493c74e8af")
                                }
                        ],
                        "socialMedias" : [
                                {
                                        "_id" : ObjectId("5e1d082641c52d4a587a9191"),
                                        "socialMediaId" : ObjectId("5e10089a3330ad05d4e1867d"),
                                        "url" : "rewrwerwerwerwerwerwerwer"
                                }
                        ],
                        "paymentTerms" : [
                                {
                                        "_id" : ObjectId("5e1d143041c52d4a587a91b7"),
                                        "paymentTermId" : ObjectId("5e1a2277bf353f493c74e8a7")
                                },
                                {
                                        "_id" : ObjectId("5e1d143041c52d4a587a91b6"),
                                        "paymentTermId" : ObjectId("5e1a228cbf353f493c74e8a8")
                                },
                                {
                                        "_id" : ObjectId("5e1d143041c52d4a587a91b5"),
                                        "paymentTermId" : ObjectId("5e1a229ebf353f493c74e8a9")
                                }
                        ],
                        "sections" : [
                                {
                                        "_id" : ObjectId("5e1e535441c52d4a587a9208"),
                                        "name" : "Camisetas",
                                        "products" : [
                                                {
                                                        "_id" : ObjectId("5e1e662f044582129844ffd5"),
                                                        "name" : "DonJuan M",
                                                        "description" : "",
                                                        "quantityAvailable" : 0,
                                                        "image" : "",
                                                        "price" : 0,
                                                        "validFrom" : ISODate("2020-01-15T01:08:49.552Z"),
                                                        "validTo" : ISODate("2020-01-15T01:08:49.552Z"),
                                                        "enabled" : true
                                                }
                                        ]
                                },
                                {
                                        "_id" : ObjectId("5e20ec889c05f229a484ea3d"),
                                        "name" : "Imãs",
                                        "products" : [
                                                {
                                                        "_id" : ObjectId("5e20ec889c05f229a484ea3e"),
                                                        "name" : "Imã",
                                                        "description" : "Imã",
                                                        "quantityAvailable" : 0,
                                                        "image" : "",
                                                        "price" : 0,
                                                        "validFrom" : ISODate("0001-01-01T00:00:00Z"),
                                                        "validTo" : ISODate("9999-12-31T00:00:00Z"),
                                                        "enabled" : true
                                                }
                                        ]
                                }
                        ]
                }
        ],
        "users" : [
                {
                        "_id" : ObjectId("5e10fc2adc147a373c312144")
                },
                {
                        "_id" : ObjectId("5e11ff8003eb832ef84342a6")
                }
        ],
        "socialMedias" : [
                {
                        "_id" : ObjectId("5e165672a2204b49c892db74"),
                        "socialMediaId" : ObjectId("5e10089a3330ad05d4e1867d"),
                        "url" : "uuuutt"
                },
                {
                        "_id" : ObjectId("5e15385fb3a0aa1004ac3598"),
                        "socialMediaId" : ObjectId("5e1009043330ad05d4e1867f"),
                        "url" : "jkkjkjkjkjk"
                }
        ],
        "sections" : [
                {
                        "_id" : ObjectId("5e15313b2e985e16ec4e7413"),
                        "name" : "Bebidas",
                        "products" : [
                                {
                                        "_id" : ObjectId("5e1e6381044582129844ffc2"),
                                        "name" : "Coca Cola Zero 2 Litros",
                                        "description" : "",
                                        "quantityAvailable" : 0,
                                        "image" : "",
                                        "price" : 18.39,
                                        "validFrom" : ISODate("1970-01-01T00:00:00Z"),
                                        "validTo" : ISODate("1970-01-01T00:00:00Z"),
                                        "enabled" : true
                                },
                                {
                                        "_id" : ObjectId("5e1e6381044582129844ffc3"),
                                        "name" : "Coca Cola 2 Litros",
                                        "description" : "",
                                        "quantityAvailable" : 0,
                                        "image" : "",
                                        "price" : 21.42,
                                        "validFrom" : ISODate("1970-01-01T00:00:00Z"),
                                        "validTo" : ISODate("1970-01-01T00:00:00Z"),
                                        "enabled" : true
                                },
                                {
                                        "_id" : ObjectId("5e1e662f044582129844ffda"),
                                        "name" : "Cerveja Heineken Lata 350ml",
                                        "description" : "Cerveja Heineken Lata 350ml",
                                        "quantityAvailable" : 0,
                                        "image" : "volkswagen-polo.jpg",
                                        "price" : 1.55,
                                        "validFrom" : ISODate("2020-01-01T00:00:00Z"),
                                        "validTo" : ISODate("1970-01-01T00:00:00Z"),
                                        "enabled" : true
                                }
                        ]
                },
                {
                        "_id" : ObjectId("5e20e8de9c05f229a484ea27"),
                        "name" : "Esfihas",
                        "products" : [
                                {
                                        "_id" : ObjectId("5e20e8de9c05f229a484ea28"),
                                        "name" : "Esfiha de carne",
                                        "description" : "Esfiha de carne",
                                        "quantityAvailable" : 0,
                                        "image" : "",
                                        "price" : 5,
                                        "validFrom" : ISODate("2020-01-01T00:00:00Z"),
                                        "validTo" : null,
                                        "enabled" : true
                                }
                        ]
                }
        ],
        "__v" : 0
}
{
        "_id" : ObjectId("5e0ffd23991918424c8d7c3b"),
        "name" : "Pizza Ruth",
        "active" : true,
        "users" : [ ],
        "socialMedias" : [ ],
        "branches" : [ ],
        "sections" : [ ],
        "__v" : 0
}
{
        "_id" : ObjectId("5e0ffd3d991918424c8d7c3c"),
        "name" : "Feijão de Corda",
        "active" : true,
        "users" : [ ],
        "socialMedias" : [ ],
        "branches" : [ ],
        "sections" : [ ],
        "__v" : 0
}

The fields validFrom and validTo (Date fields) from the collection products nested in branches.sections need to be converted to the format yyyy-mm-dd. I can do that with this aggregation pipeline:

{ $unwind: { path: "$branches.sections", preserveNullAndEmptyArrays: true } },   
{ 
    "$addFields": {
        "branches.sections.products": { 

            $map: {
                input: "$branches.sections.products",
                as: "product",
                in: {
                    '_id': "$$product._id",
                    'name': "$$product.name",
                    'description': "$$product.description",
                    'quantityAvailable': "$$product.quantityAvailable",
                    'image': "$$product.image",
                    'imageUrl': "$$product.imageUrl",
                    'price': "$$product.price",
                    'validFrom' : {"$dateToString": { "date": "$$product.validFrom", "format": "%Y-%d-%m" }},
                    'validTo' : {"$dateToString": { "date": "$$product.validTo", "format": "%Y-%d-%m" }},
                    'enabled': "$$product.enabled",
                }
            }
        }
    }
}

I can successfully convert those date fields, but I need now to "re-unwind" the array products, in order to be just like before the unwind.

Any clue in how to proceed? Or even a different way to format those dates without having to unwind? Tried dozens of ways of $group, but without any success.

Upvotes: 1

Views: 595

Answers (1)

Ashh
Ashh

Reputation: 46461

You just need to $map over each nested array to drill upload the validTo and validFrom field

.aggregate([
  { "$addFields": {
    "branches": {
      "$map": {
        "input": "$branches",
        "as": "branch",
        "in": {
          "$mergeObjects": [
            "$$branch",
            { "section": {
              "$map": {
                "input": "$$branch.sections",
                "as": "section",
                "in": {
                  "$mergeObjects": [
                    "$$section",
                    { "product": {
                      "$map": {
                        "input": "$$section.products",
                        "as": "product",
                        "in": {
                          "$mergeObjects": [
                            "$$product",
                            {
                              "validFrom": { "$dateToString": { "date": "$$product.validFrom", "format": "%Y-%d-%m" }},
                              "validTo": { "$dateToString": { "date": "$$product.validTo", "format": "%Y-%d-%m" }}
                            }
                          ]
                        }
                      }
                    }}
                  ]
                }
              }
            }}
          ]
        }
      }
    }
  }}
])

MongoPlayground

Upvotes: 1

Related Questions