写入 Spring 数据 Mongo 空查询以从 mongo 获取 NULL 文档?
Write Spring Data Mongo Null query to get NULL documents from mongo?
我正在开发 Spring + Spring Data Mongo
示例。在此示例中,我想获取 Region is NULL
处的文档。为此,我开发了 Repository 方法
List<Customer> findByRegionNull();
数据库查询显示大写 NULL 而不是 Null,现在当 db.customers.find({ "Region" : "null" }); 查询执行 我没有看到任何结果。 我们如何编写存储库查询来获取 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"
}
我开发的代码:
CustomerRepository.java
public interface CustomerRepository extends CrudRepository<Customer, String>{
List<Customer> findByRegionNull();
}
我的测试用例没有给出结果?
@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
}
只是你需要使用@Query
注解方式。请refer 6.3.2
:
http://docs.spring.io/spring-data/data-document/docs/current/reference/html/
使用这个:
public interface CustomerRepository extends CrudRepository<Customer, String>{
@Query("{ 'Region' : 'NULL' }")
List<Customer> findByRegionNull();
}
我正在开发 Spring + Spring Data Mongo
示例。在此示例中,我想获取 Region is NULL
处的文档。为此,我开发了 Repository 方法
List<Customer> findByRegionNull();
数据库查询显示大写 NULL 而不是 Null,现在当 db.customers.find({ "Region" : "null" }); 查询执行 我没有看到任何结果。 我们如何编写存储库查询来获取 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"
}
我开发的代码: CustomerRepository.java
public interface CustomerRepository extends CrudRepository<Customer, String>{
List<Customer> findByRegionNull();
}
我的测试用例没有给出结果?
@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
}
只是你需要使用@Query
注解方式。请refer 6.3.2
:
http://docs.spring.io/spring-data/data-document/docs/current/reference/html/
使用这个:
public interface CustomerRepository extends CrudRepository<Customer, String>{
@Query("{ 'Region' : 'NULL' }")
List<Customer> findByRegionNull();
}