Reputation:
I am developing Spring + Spring Data Mongo
example. In this example I'd like to get document where Region is NULL
. For this I developed Repository method
List<Customer> findByRegionNull();
database query shows capital NULL and not Null, Now when db.customers.find({ "Region" : "null" }); query execute I don't see any results. How we can write repository query to get NULL?
db.customers.find({ "Region" : "NULL" });
{
"_id" : ObjectId("51ba0970ae4ad8cc43bb95e3"),
"CustomerID" : "ALFKI",
"CompanyName" : "Alfreds Futterkiste",
"ContactName" : "Maria Anders",
"ContactTitle" : "Sales Representative",
"Address" : "Obere Str. 57",
"City" : "Berlin",
"Region" : "NULL",
"PostalCode" : 12209,
"Country" : "Germany",
"Phone" : "030-0074321",
"Fax" : "030-0076545"
}
{
"_id" : ObjectId("51ba0970ae4ad8cc43bb95e4"),
"CustomerID" : "ANATR",
"CompanyName" : "Ana Trujillo Emparedados y helados",
"ContactName" : "Ana Trujillo",
"ContactTitle" : "Owner",
"Address" : "Avda. de la Constitución 2222",
"City" : "México D.F.",
"Region" : "NULL",
"PostalCode" : 5021,
"Country" : "Mexico",
"Phone" : "(5) 555-4729",
"Fax" : "(5) 555-3745"
}
The code I developed: CustomerRepository.java
public interface CustomerRepository extends CrudRepository<Customer, String>{
List<Customer> findByRegionNull();
}
My Test case Doesnot give results?
@Test
public void testRegionNull(){
List<Customer> customers =cService.findByRegionNull();
LOGGER.debug("SIZE : "+customers.size());
}
Customer.java
@Document(collection="customers")
public class Customer {
@Id
private ObjectId id;
@Field("CustomerID")
private String customerId;
@Field("CompanyName")
private String companyName;
@Field("ContactName")
private String contactName;
@Field("ContactTitle")
private String contactTitle;
@Field("Address")
private String address;
@Field("City")
private String city;
@Field("Region")
private String region;
@Field("PostalCode")
private String postalCode;
@Field("Country")
private String country;
@Field("Phone")
private String phone;
@Field("Fax")
private String fax;
// setters and getters
}
Upvotes: 0
Views: 4236
Reputation: 11975
Simply you need to use @Query
annotation way. Please refer 6.3.2
:
http://docs.spring.io/spring-data/data-document/docs/current/reference/html/
Use this:
public interface CustomerRepository extends CrudRepository<Customer, String>{
@Query("{ 'Region' : 'NULL' }")
List<Customer> findByRegionNull();
}
Upvotes: 1