I have a collection of user's data with a friends list that has a nested array of objects.
{username: "abcd", friends: [{name: "A", status : "active", code: "X1"},{name: "B", status : "inactive", code: "X2"}, {name: "B", status : "active", code: "X3"}]}
{username: "xyz", friends: [{name: "A", status : "active", code: "X1"},{name: "E", status : "inactive", code: "X2"}, {name: "F", status : "active", code: "X3"}]}
{username: "xyz", friends: [{name: "X", status : "active", code: "X1"},{name: "E", status : "inactive", code: "X2"}, {name: "F", status : "active", code: "X3"}]}
I am trying to find a query with who are have friends name is "A".
I am getting two records with all data.
I tried using the below query.
users.find({"friends.name" : "A"});
Current output:
{username: "abcd", friends: [{name: "A", status : "active", code: "X1"},{name: "B", status : "inactive", code: "X2"}, {name: "B", status : "active", code: "X3"}]}
{username: "xyz", friends: [{name: "A", status : "active", code: "X1"},{name: "E", status : "inactive", code: "X2"}, {name: "F", status : "active", code: "X3"}]}
Expected Output:
{username: "abcd", friends: [{name: "A", status : "active", code: "X1"}]}
{username: "xyz", friends: [{name: "A", status : "active", code: "X1"}]}
The result should return only who have name is "A".. Other objects data should not return..
Can you help to build query like above output.