1

I need to filter my business document according to a type of service.

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);

And Schema service is:

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

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

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

Of this array of businesses would only need those that have within its services the "Service A" for example.

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

        }
]

In this case the result I want to get is:

{
            "_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
                }
            ],   

        }

It is very important for me to be able to carry out this search, because the system will allow the user to choose as a filter the business that has the desired service.

It's my first project on nodejs + mongodb and I would like your help before continuing on. Thank you very much

1 Answers1

0

Using $lookup, here's what you can achieve:

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

In this aggregation pipeline, you first match and find service documents with name "Service A". Then lookup in service array of business documents for matching ones and place them in businesses field. The result will look like this:

{
    "_id" : "594af96883ab76db8ecd021b",
    "name" : "Service A",
    "businesses" : [ 
        {
            "_id" : "594af9a683ab76db8ecd0225",
            "slug" : "Almacene2s",
            "owner" : "59482e80d4df7208503154b8",
            "name" : "Almacene2s",
            "service" : [ 
                "594af96883ab76db8ecd021b", 
                "594af96883ab76db8ecd021d"
            ]
        }
    ]
}
Talha Awan
  • 4,573
  • 4
  • 25
  • 40