首页 > 解决方案 > Spring Data & Couchbase - 查找没有特定字段的文档

问题描述

在我的 Spring Boot 应用程序中,我有一个存放文档的 couchbase 存储桶。

我使用扩展的接口 CRUD 存储桶CouchbasePagingAndSortingRepository

我正在尝试创建一个查询来选择所有没有特定字段的文档。

所以假设我们有这些文件并且我们想找到那些没有“颜色”的文件:

{
 "type":"Cat"
 "name":"Oscar",
 "color":"red"
}

{
 "type":"Cat"
 "name":"Polly"
}

我尝试在我的界面中创建几个方法:

public interface PetRepository<Pet, S> extends CouchbasePagingAndSortingRepository<Pet, String> {

List<Pet> findAllByTypeAndColorNull(String type);

List<Pet> findAllByTypeAndColorIsNull(String type);

List<Pet> findAllByTypeAndColorFalse(String type);

但是它们都返回空列表而不是预期的文档。

标签: spring-bootspring-datacouchbasen1ql

解决方案


问题是您的文档中根本不存在此字段,因此正确的方法是使用MISSING关键字: https ://docs.couchbase.com/server/5.5/n1ql/n1ql-language-reference/comparisonops.html

@Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and companyId = $1 and (yourAtrribute IS NULL OR yourAtrribute IS MISSING )")
List<Checklist> listCheckCompanyId(String companyId);

推荐阅读