I have a MongoRepository query method which needs to fetch data based on conditions.
I have the following record in database.
{
'name' : 'test',
'show' : false,
'free' : true
}
And the following query, But the query doesn't return this record.
repositiry.findByNameNotNullAndShowIsTrueOrFreeIsTrue()
as per the condition, Name
is not null
and Free
is True
. But Why am I not getting the record.
I reproduced your scenario. It's working here. The query logged was correct.
{
"$or": [
{
"name": {
"$ne": null
},
"show": true
},
{
"free": true
}
]
}
To enable the mongodb query logs you must DEBUG MongoTemplate.
logging.level.org.springframework.data.mongodb.core.MongoTemplate = DEBUG
Entity
@Document
@Data
@Builder
class Entity {
private String id;
private String name;
private boolean show;
private boolean free;
}
Repository
interface EntityRepository extends MongoRepository<Entity,String> {
List<Entity> findByNameIsNotNullAndShowIsTrueOrFreeIsTrue();
}
Test
@Test
public void testQuery() {
repository.deleteAll();
Entity entity = Entity.builder()
.free(true)
.show(false)
.name("test")
.build();
repository.save(entity);
List<Entity> entities = repository.findByNameIsNotNullAndShowIsTrueOrFreeIsTrue();
Assert.assertEquals(1, entities.size());
}
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.