user3211705
user3211705

Reputation: 2578

How to get particular value of inner Array along with main values in Mongoose

I have a collection as shown below

[
    {
        "_id":"564d747ce27a66cd5aa6fdf2",
        "Name":"Person1",
        "Role":[
            {
                "_id":"564d7253e27a66cd5aa6fdee",
                "RoleName":"AAA",
                "Rel":[
                    {
                        "_id":"564d728be27a66cd5aa6fdf0",
                        "Name":"Lead"
                    }
                ]
            },
            {
                "_id":"564d7253e27a66cd5aa6fdef",
                "RoleName":"BBB",
                "Rel":[
                    {
                        "_id":"564d728be27a66cd5aa6fdf0",
                        "Name":"Member"
                    }
                ]
            }
        ]
    },
    {
        "_id":"564d747ce27a66cd5aa6fdf2",
        "Name":"Person2",
        "Role":[
            {
                "_id":"564d7253e27a66cd5aa6fdee",
                "RoleName":"AAA",
                "Rel":[
                    {
                        "_id":"564d728be27a66cd5aa6fdf0",
                        "Name":"Lead"
                    }
                ]
            }
        ]
    }
]

Now, I want to check for a Role id and get its related 'Rel' along with the Name as shown below:

[
    {
        "_id":"564d747ce27a66cd5aa6fdf2",
        "Name":"Person1",
        "Role":[
            {
                "_id":"564d7253e27a66cd5aa6fdee",
                "RoleName":"AAA",
                "Rel":[
                    {
                        "_id":"564d728be27a66cd5aa6fdf0",
                        "Name":"Lead"
                    }
                ]
            }
        ]
    },
    {...}
]

I have used Mongoose to fetch data as shown below:

exports.findone = function(req, res) {
    var id = req.params.id;
    model1.find({'Role._id': id}).select('Name Role').exec(function(err, results) {
         res.send(results));    
  });
};

Here, When I send the Role Id as input it fetches the person that have the mentioned role id. But with in 'Role' node I get all the roles associated with it. Below is the output that I get for the above query.

[
    {
        "_id":"564d747ce27a66cd5aa6fdf2",
        "Name":"Person1",
        "Role":[
            {
                "_id":"564d7253e27a66cd5aa6fdee",
                "RoleName":"AAA",
                "Rel":[
                    {
                        "_id":"564d728be27a66cd5aa6fdf0",
                        "Name":"Lead"
                    }
                ]
            },
            {
                "_id":"564d7253e27a66cd5aa6fdef",
                "RoleName":"BBB",
                "Rel":[
                    {
                        "_id":"564d728be27a66cd5aa6fdf0",
                        "Name":"Member"
                    }
                ]
            }
        ]
    }
]

I want to get the particular role and not all the roles to be displayed. Can you help me to get this done...

Upvotes: 0

Views: 92

Answers (1)

João Antunes
João Antunes

Reputation: 571

You can use the select operation together with a $elemMatch so that only the role you want is presented as stated in the MongoDB docs.

Your data fetch would look something like this:

exports.findone = function(req, res) {
    var id = req.params.id;
    model1.find({'Role._id': id}).select({Name: 1, Role: {$elemMatch: {_id: id}}}).exec(function(err, results) {
         res.send(results));    
  });
};

Upvotes: 1

Related Questions