如何在猫鼬中进行聚合和查找过滤?

时间:2019-12-20 19:40:51

标签: mongodb mongoose

我正在使用以下代码:

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

      },

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

,输出为:

[{
        "_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": "doctor1@gmail.com",
                "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": "doctor1@gmail.com",
                "phone": "0234820",
                "city": "Faridabad",
                "password": "$2a$10$m/FZg5XG9tVt86FCRAc8fO0RWhcs9D.QLFIH7BQqP/wuOR7EX8OuG",
                "problem": "Neurology",
                "usertype": 1,
                "saltSecret": "$2a$10$m/FZg5XG9tVt86FCRAc8fO",
                "__v": 0
            }
        ]
    }
]

我要求只获得"userid" : "5dfcffdb2c5be31b803313ef"的猫鼬。我该怎么办。

输出应为:

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

    ]

1 个答案:

答案 0 :(得分:1)

您需要在$match阶段之前将$lookup用作初始阶段,以便过滤所需文档的集合并从其他集合中获取引用的一个或多个文档:

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

}])

如果userIdObjectId()

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

参考: $match