2

I want a query in mongo db like this one in SQL:

Select * from Users 
where familyId =@X and (isDeleted =false or isDeleted is null)

already I have first condition, I don't know how to mixed it with And-Or

var myMembers = Meteor.users.find({ "profile.family_id":      Meteor.user().profile.family_id });

how should it be possible?

chridam
  • 100,957
  • 23
  • 236
  • 235
Mehrnoosh
  • 879
  • 2
  • 12
  • 27

1 Answers1

6

You can explicitly query with the $and operator as:

var family_id = Meteor.user().profile.family_id,
    myMembers = Meteor.users.find({ 
        "$and": [
            { "profile.family_id": family_id },
            {
                "$or": [
                    { "isDeleted": false },
                    { "isDeleted": null } /* or { "isDeleted": { "$exists": false } } */
                ]
            }
        ]
    });

or implicitly by specifying comma-separated expressions:

var family_id = Meteor.user().profile.family_id,
    myMembers = Meteor.users.find({ 
        "profile.family_id": family_id,
        "$or": [
            { "isDeleted": false },
            { "isDeleted": null } /* or { "isDeleted": { "$exists": false } } */
        ]
    });

Note: To check whether a field exists, you can use the $exists operator as { "isDeleted": { "$exists": false } } since the { isDeleted : null } query matches documents that either contain the isDeleted field whose value is null or that do not contain the isDeleted field.

chridam
  • 100,957
  • 23
  • 236
  • 235