简体   繁体   中英

Query mongodb using Morphia

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM