Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Excract only the lookup's return (mongoose)

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading