Reputation: 21368
I need to query MongoDB and I am using Morphia to do that. I need to do something like this
select * from table_name where (column_1== null or column_1== value1) AND (column_2= value2 or column_3= value3).
I tried this but it didn't work.
query.and(
query.or(
query.criteria(field1).equal(value1),
query.criteria(field1).equal(null)
),
query.or(
query.criteria(field2).equal(value2),
query.criteria(field3).equal(value3)
)
);
Also, below is the Mongodb query for the above
db.FILE_JOURNEY.find( {$and :[ {
$or: [ { SUBSCRIBERID: "225136298" }, { SUBSCRIBERID : null} ]
},
{
$or: [ { BATCHID : "615060299" }, { FILENAME : "TR.NYHBE.834Q.D.212311980342.QHP.dat" } ]
}
]
}
)
What things can I try next?
Upvotes: 0
Views: 390
Reputation: 9695
If you try something like this:
query.and(
query.or(
query.criteria(field1).equal(value1),
query.criteria(field1).doesNotExist()
),
query.or(
query.criteria(field2).equal(value2),
query.criteria(field3).equal(value3)
)
);
MongoDB/Morphia doesn't save fields with null values - it just skips that field, so the query for field=NULL would never match hence the doesNotExist
. Strange that it doesn't give an warning. Do you have disableValidation() enabled?
Upvotes: 0