spring-boot - 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);
但是它们都返回空列表而不是预期的文档。
解决方案
问题是您的文档中根本不存在此字段,因此正确的方法是使用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);
推荐阅读
- routing - Istio / Kuberentes 中的用户特定路由
- c++ - 具有伪继承设计 C++ 的模板
- python - 如何在不更改字节的情况下将字符串转换为浮点数?
- angular - 表单的复选标记验证
- android - 如何查看数据库中的数据 (REST)
- c# - FTD2xx_NET DLL 是托管代码还是非托管代码?
- solr - 使用 bin/post 和托管模式突出显示 Solr 搜索结果
- svn - Tortoise SVN添加文件夹没有文件问题
- c# - 具有远程 Web UI(如 utorrent)的桌面 WPF 应用程序
- keras - 可视化深度网络时的最佳迭代次数