0

I am trying to find a specific object from array of objects in mongodb.

I am trying this

Company.findOne ({
"configuration.macAddress": "AB-90-dF-8d"
});

It returns me the exact company but it returns all the configuration array I want only configuration with matching macAddress

2 Answers2

1

Instead use aggregate(). $unwind the configuration array first, then you can $match the specific element only.

Company.aggregate([
    {
        "$unwind": "$configuration"
    },
    {
        "$match":{
            "configuration.macAddress": "AB-90-dF-8d"
        }
    }
]);
Wake
  • 1,686
  • 10
  • 14
0

you can use $elemMatch to find a particular object in an array.

Company.find({ configuration: { $elemMatch: { macAddress: "AB-90-dF-8d"} } } );

can you show me your array of objects?