1024programmer Java MeteorMongoDB query using AND and OR

MeteorMongoDB query using AND and OR

I want to use the query in mongo db in SQL:

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

Already I have the first condition, I don’t know how to mix it with the And-Or

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

How should it be possible?

1> chridam..:


You can use the $and operator Explicit query:

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 a comma-separated expression:

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, { "isDeleted": { "$ exists": false } }Because { isDeleted : null } the query matches fields containing isDeleted whose value is null or does not contain the isDeleted field Documentation of the field.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/763148

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索