Mongoose: Filter collection based on values in another collection

扶醉桌前 提交于 2019-12-08 08:41:30

You can do this using aggregate query:

db.collectionA.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $lookup: {
                from: "collectionB",
                localField: "_id",
                foreignField: "_id",
                as: "collectionBData"
            }
        },

        // Stage 2
        {
            $unwind: {
                path : "$collectionBData"
            }
        },

        // Stage 3
        {
            $match: {
                "collectionBData.email": "your@email.address"
            }
        },

    ]

);

Hope this solves your query.

Also, in Mongo, we use naming conventions as collections instead of table and _id instead of id.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!