Hongbo Miao
Hongbo Miao

Reputation: 49984

How to do query correctly in this case?

In the database, there are two users,

// User1

    {ABC: [{aa:"11", bb:"11"}, {aa:"22", bb:"22"}]}

// User2

    {ABC: [{aa:"11", bb:"22"}, {aa:"33", bb:"44"}]}

I want to get users whose aa = 11, and bb = 22 at the same time in one element of the array. So I only want to get User2 here.

let users = Meteor.users.find({$and: [{'ABC.aa': "11"}, {'ABC.bb': "22"}]});

But now I got both users using this query. How can I write the query correctly? Thanks

Upvotes: 1

Views: 22

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20256

Use $elemMatch to match an entire element:

let users = Meteor.users.find({ ABC: { $elemMatch: { aa: "11", bb: "22" }}});

Upvotes: 2

Related Questions