如何在填充的mongodb之后进行过滤

时间:2017-06-21 21:59:49

标签: node.js mongodb mongoose mongoose-schema

我需要根据服务类型过滤我的商业文档。

var BusinessSchema = Schema({
    name: String,
    slug: String,
    service: [{type: Schema.ObjectId, ref: 'Service'}],
    owner: {type: Schema.ObjectId, ref: 'User'}

});

module.exports = mongoose.model('Business', BusinessSchema);

Schema服务是:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ServiceSchema =  Schema({
    name: String,
});

module.exports = mongoose.model('Service', ServiceSchema);

在这一系列的企业中,只需要那些在其服务范围内拥有服务A"服务A"例如。

"business": [
        {
            "_id": "594957df4e195d2500324726",
            "owner": "59482e80d4df7208503154b8",
            "slug": "Almacene2s",
            "name": "Almacene2s",
            "__v": 0,
            "service": [
                {
                    "_id": "594ab160778ae82c44af3a78",
                    "name": "Service C",
                    "__v": 0
                },
                {
                    "_id": "594ab1be778ae82c44af3a7a",
                    "name": "Service D",
                    "__v": 0
                }
            ], 

        },
        {
            "_id": "5948483e6bcc1f2788b09145",
            "owner": "59482e80d4df7208503154b8",
            "slug": "guaranda-2",
            "name": "Guaranda Fig",

            "service": [
                {
                    "_id": "594ab160778ae82c44af3a78",
                    "name": "Service A",
                    "__v": 0
                },
                {
                    "_id": "594ab1be778ae82c44af3a7a",
                    "name": "Service B",
                    "__v": 0
                }
            ],   

        }
]

在这种情况下,我想得到的结果是:

{
            "_id": "5948483e6bcc1f2788b09145",
            "owner": "59482e80d4df7208503154b8",
            "slug": "guaranda-2",
            "name": "Guaranda Fig",

            "service": [
                {
                    "_id": "594ab160778ae82c44af3a78",
                    "name": "Service A",
                    "__v": 0
                },
                {
                    "_id": "594ab1be778ae82c44af3a7a",
                    "name": "Service B",
                    "__v": 0
                }
            ],   

        }

能够执行此搜索非常重要,因为系统将允许用户选择具有所需服务的业务过滤器。

这是我在nodejs + mongodb上的第一个项目,在继续之前我希望得到你的帮助。非常感谢你

1 个答案:

答案 0 :(得分:0)

使用$lookup,这是您可以实现的目标:

Service.aggregate([
    {
        $match: {
            "name": "Service A"
        }
    },
    {
        $lookup: {
            from: "business",
            localField: "_id",
            foreignField: "service",
            as: "businesses"
        }
    }
], function(err, result) {
    console.log(result);
});

在此聚合管道中,您首先匹配并查找名称为"服务A"的服务文档。然后在service业务文档数组中查找匹配的文档,并将它们放在businesses字段中。结果将如下所示:

{
    "_id" : "594af96883ab76db8ecd021b",
    "name" : "Service A",
    "businesses" : [ 
        {
            "_id" : "594af9a683ab76db8ecd0225",
            "slug" : "Almacene2s",
            "owner" : "59482e80d4df7208503154b8",
            "name" : "Almacene2s",
            "service" : [ 
                "594af96883ab76db8ecd021b", 
                "594af96883ab76db8ecd021d"
            ]
        }
    ]
}