我已經移植了一些Entity
JPA
來記錄和現在移植我的一些查詢。 這裏是JPA查詢:下面使用彈簧數據將JPQL移植到mongo的查詢mongo標準
em.createQuery("select distinct c from CustomerImpl c left join fetch c.addresses ca where (:name is null or c.firstName LIKE :name or c.lastName LIKE :name) and (:ref is null or c.externalReference LIKE :ref) and (:city is null or ca.city LIKE :city) order by c.firstName").setParameter("name", name).setParameter("ref", customerRef).setParameter("city", city).getResultList();
是我的嘗試:
Criteria orNameCriteria = new Criteria().orOperator(Criteria.where("firstName").is(null), Criteria.where("firstName").is(name), Criteria.where("lastName").is(name));
Criteria orCustomerRefCriteria = new Criteria().orOperator(Criteria.where("externalReference").is(null), Criteria.where("externalReference").regex(customerRef,"i"));
Criteria orAddress = new Criteria().orOperator(Criteria.where("addresses.city").is(null), Criteria.where("addresses.city").regex(city, "i"));
Query nameq = new Query(new Criteria().andOperator(orNameCriteria,orCustomerRefCriteria,orAddress));
該查詢返回零大小數組列表。然後我將orNameCriteria
更改爲使用is子句,並確保name
變量中包含的數據具有/
作爲後綴和前綴。這並不奏效。
但查詢從mongoVue
和RockMongo
客戶:
{ firstName: /SAM/}
返回數據。
問題1:你怎麼寫like子句與彈簧數據蒙戈Criteria
?
問題2:是正確的方式來使用或並和條款與criteria
感謝您閱讀