Tell Me How
Tell Me How

Reputation: 570

How to filter with aggregation and lookup in mongoose?

I am using the following code:

Appointment
    .aggregate([
        {
          $lookup:
            {
              from: "users",
              localField: "doctorId",
              foreignField: "_id",
              as: "appointment_book"
            },    

      },

    ])
      .then((task) => res.send(task))
      .catch((error) => console.log(error));
});

and the output is:

[{
        "_id": "5dfd17823a974b39d89857d0",
        "userId": "5dfcff8d2c5be31b803313ee",
        "doctorId": "5dfd00102c5be31b803313f0",
        "dateOfAppointment": "22/12/2019",
        "timeOfAppointment": "11",
        "currentStatus": "Pending",
        "remarks": "",
        "__v": 0,
        "appointment_book": [
            {
                "_id": "5dfd00102c5be31b803313f0",
                "name": "doctor1",
                "avatar": "http://localhost:4000/public/doctor-02.jpg",
                "email": "[email protected]",
                "phone": "0234820",
                "city": "Faridabad",
                "password": "$2a$10$m/FZg5XG9tVt86FCRAc8fO0RWhcs9D.QLFIH7BQqP/wuOR7EX8OuG",
                "problem": "Neurology",
                "usertype": 1,
                "saltSecret": "$2a$10$m/FZg5XG9tVt86FCRAc8fO",
                "__v": 0
            }
        ]
    },
    {
        "_id": "5dfd17bf3a974b39d89857d2",
        "userId": "5dfcffdb2c5be31b803313ef",
        "doctorId": "5dfd00102c5be31b803313f0",
        "dateOfAppointment": "23/12/2019",
        "timeOfAppointment": "11",
        "currentStatus": "Pending",
        "remarks": "",
        "__v": 0,
        "appointment_book": [
            {
                "_id": "5dfd00102c5be31b803313f0",
                "name": "doctor1",
                "avatar": "http://localhost:4000/public/doctor-02.jpg",
                "email": "[email protected]",
                "phone": "0234820",
                "city": "Faridabad",
                "password": "$2a$10$m/FZg5XG9tVt86FCRAc8fO0RWhcs9D.QLFIH7BQqP/wuOR7EX8OuG",
                "problem": "Neurology",
                "usertype": 1,
                "saltSecret": "$2a$10$m/FZg5XG9tVt86FCRAc8fO",
                "__v": 0
            }
        ]
    }
]

My requirement to get only "userid" : "5dfcffdb2c5be31b803313ef" in mongoose. How can I do this.

output should be:

         {
            "_id": "5dfd17bf3a974b39d89857d2",
            "userId": "5dfcffdb2c5be31b803313ef",
            "doctorId": "5dfd00102c5be31b803313f0",
            ....
            "appointment_book": [
                {
                    "_id": "5dfd00102c5be31b803313f0",
                    "name": "doctor1",
                    ....
                }
            ]
        },
       {
            "_id": "5dfd17bf3a974b39d89857e3",
            "userId": "5dfcffdb2c5be31b803313ef",
            "doctorId": "5dfd00102c5be31b803313g1",
            ....
            "appointment_book": [
                {
                    "_id": "5dfd00102c5be31b803313ff",
                    "name": "doctor2",
                    ....
                }
            ]
        },

    ]

Upvotes: 0

Views: 63

Answers (1)

whoami - fakeFaceTrueSoul
whoami - fakeFaceTrueSoul

Reputation: 17935

You need to use $match as initial stage before $lookup stage in order to filter collection for the document needed & get referenced document/documents from other collection :

Appointment.aggregate([
{ $match: { "userId": "5dfcffdb2c5be31b803313ef" } },
{
    $lookup:
    {
        from: "users",
        localField: "doctorId",
        foreignField: "_id",
        as: "appointment_book"
    },

}])

In case of userId being an ObjectId() :

const mongoose = require('mongoose');
const userId = mongoose.Types.ObjectId("5dfcffdb2c5be31b803313ef");

Appointment.aggregate([
{ $match: { "userId": userId } },
{
    $lookup:
    {
        from: "users",
        localField: "doctorId",
        foreignField: "_id",
        as: "appointment_book"
    }
}])

Ref : $match

Upvotes: 1

Related Questions