Spring-Mongo $ne 支持:如何在按字段查找方法中使用 $ne?

Spring-Mongo $ne support : How to use $ne in find by field methods?

我在Spring-boot中有一个mongo集合模型如下

@Document
class MyModel{
       @Id
       String id;
       String name;
}

我创建了它的存储库

public interface MyModelRepository extends MongoRepository<MyModel,String> {

}

我知道我可以在 MyModelRepository 中声明一个方法,使用 findByField(String name) 查找 name 值等于某个名称(让 John)的文档。但是,如果我想要名称不等于 John 的所有文档,那么我不确定如何在同一存储库中声明此方法。

我知道我可以将 @Query 或 Criteria 与 MongoTemplate 一起使用,但我想知道是否可以不使用 @Query 或标准,就像 findBy 等值一样。我尝试了 List<MyModel> findByNotEqualName(String name)List<MyModel> findByNotName(String name) 但两者都抛出错误。我试图搜索它但没有找到任何东西,Found this 但也使用 Criteria 和 MongoTemplate 来回答这个问题。

NOT ($ne) 运算符应该在 field 名称之后。所以正确的语法是

public interface MyModelRepository extends MongoRepository<MyModel,String> {
         List<MyModel> findByNameNot(String name);
}