Home > Blockchain >  Excract only the lookup's return (mongoose)
Excract only the lookup's return (mongoose)

Time:04-12

So I have two collections in my mongodb.

   // Collection 1, named Event
   {
        "_id": "625335297fcc20782bcdf085",
        "title": "Evento 1",
        "description": "lorem ipsum evento 1",
        "img": "nao implementado",
        "value": 0,
        "remainingVacancies": 14,
        "isSingleDay": false,
        "dateByDay": [
            {
                "initialDate": "2022-04-10T21:17:09.785Z",
                "finalDate": "2022-04-10T21:17:09.785Z",
                "_id": "62534955171ecadae7ff6cd4"
            },
            {
                "initialDate": "2022-04-10T21:17:09.785Z",
                "finalDate": "2022-04-10T21:17:09.786Z",
                "_id": "62534955171ecadae7ff6cd5"
            }
        ],
        "__v": 0
    }

and

   // Collection 2, named Presentation
   {
        "_id": "62549d4d3b41827fe7593332",
        "title": "Apresentação 1",
        "description": "lorem ipsum 11 testee",
        "img": "nao implementado",
        "value": 0,
        "remainingVacancies": 14,
        "isSingleDay": true,
        "dateByDay": [
            {
                "initialDate": "2022-04-11T21:27:41.823Z",
                "finalDate": "2022-04-11T21:27:41.823Z",
                "_id": "62549d4d3b41827fe7593333"
            }
        ],
        "eventId": "625335297fcc20782bcdf085",
        "__v": 0
    }

I've tried to use lookup aggregation in Event like this:

Event.aggregate([
    {
      $match: {
        _id: mongoose.Types.ObjectId(_id)
      },
    },
    {
      $lookup: {
        from: 'presentations',
        localField: '_id',
        foreignField: 'eventId',
        as: 'registeredPresentations'
      },
    },
    {
      $unwind: "$registeredPresentations"
    },
    {
      $project: {
        _id: 0,
        registeredPresentations: 1
      }
    },
  ]);

It returns this output

   {
        "registeredPresentations": {
            "_id": "6254900aa5b5977e99498ee0",
            "title": "Apresentação 1",
            "description": "lorem ipsum 22 testee",
            "img": "nao implementado",
            "value": 0,
            "remainingVacancies": 14,
            "isSingleDay": true,
            "dateByDay": [
                {
                    "initialDate": "2022-04-11T20:31:06.324Z",
                    "finalDate": "2022-04-11T20:31:06.324Z",
                    "_id": "6254900aa5b5977e99498ee1"
                }
            ],
            "eventId": "625335297fcc20782bcdf085",
            "__v": 0
        }
    },

But since I have multiples presentations related in one Event, I don't want the value returns as a object of "registeredPresentations". What I want is use lookup to extract the return without the "registeredPresentations" in the output, just like that:

   {
        "_id": "62549d4d3b41827fe7593332",
        "title": "Apresentação 1",
        "description": "lorem ipsum 11 testee",
        "img": "nao implementado",
        "value": 0,
        "remainingVacancies": 14,
        "isSingleDay": true,
        "dateByDay": [
            {
                "initialDate": "2022-04-11T21:27:41.823Z",
                "finalDate": "2022-04-11T21:27:41.823Z",
                "_id": "62549d4d3b41827fe7593333"
            }
        ],
        "eventId": "625335297fcc20782bcdf085",
        "__v": 0
    },

There's a way to extract this value without the "registeredPresentations" in the output?

CodePudding user response:

Use $replaceWith

db.orders.aggregate([
  {
    $match: {
      _id: "625335297fcc20782bcdf085"
    }
  },
  {
    $lookup: {
      from: "presentations",
      localField: "_id",
      foreignField: "eventId",
      as: "registeredPresentations"
    }
  },
  {
    $unwind: "$registeredPresentations"
  },
  {
    $project: {
      _id: 0,
      registeredPresentations: 1
    }
  },
  {
    $replaceWith: "$registeredPresentations"
  }
])

mongoplayground

  • Related