How to specify Multiple conditions in Java Mongo

3.5k views Asked by At

I have a code which requires me to get user details from a mongo database collection

Following is one of the field in my Users collection

"emailSettings" : {
    "flag1" : true,
    "flag2" : true,
    "flag3" : true,
},

I'm parsing the Mongo collection from my java program using the BasicDB object and I would want to get all the entries which satisfy the emailSettings.flag1 =true and emailSettings.flag2 = true.

When I use BasicDBObject to do this, I'm able to get entries satisfying only one of the conditions.

Is there a solution for this ?

1

There are 1 answers

2
pingw33n On

In Mongo query parts are combined with 'and' operator by default. So simply doing this

DBObject query = BasicDBObjectBuilder.start("emailSettings.flag1", true).add("emailSettings.flag2", true).get();

should give you expected result.