Spring数据mongodb存储库。如何通过ID列表进行搜索?
我有下面的课Spring数据mongodb存储库。如何通过ID列表进行搜索?,spring,mongodb,spring-data,spring-data-mongodb,Spring,Mongodb,Spring Data,Spring Data Mongodb,我有下面的课 public class Task{ ObjectId id; String title; String description; /* Getters and Setters removed for brevity */ } 我有以下mongoRepository类,非常简单: public interface TaskRepository extends MongoRepository<Task, String> { } pu
public class Task{
ObjectId id;
String title;
String description;
/* Getters and Setters removed for brevity */
}
我有以下mongoRepository类,非常简单:
public interface TaskRepository extends MongoRepository<Task, String> {
}
public interface TaskRepository扩展了MongoRepository{
}
正如您所看到的,我还没有尝试扩展这个类-如果我想有一个find方法,在这里我想做什么,我可以给它一个id列表,然后取回我的相应任务列表?
crudepository
whichMongoRepository
extends有一个方法,它采用Itereable
我想这正是你想要的
请注意,在最新的里程碑版本中,它被重命名为。您可以创建一个自定义查询方法来搜索
\u id
值的数组:
@Query(value = "{ '_id' : {'$in' : ?0 } }", fields = "{ 'description': 0 }")
Iterable<Task> findAllThin(Iterable<String> ids);
@Query(value=“{'\'id':{'$in':?0}”,fields=“{'description':0}”)
Iterable findAllThin(Iterable ID);
(在这种情况下,它仅返回字段id
和title
)
@Neil Lunn给了我答案。
@Query({'u id':'in':?0}'))
使用合适的查找方法?没有基于mongoreposity的项目,但你给了你一个开始查找的好地方。该死,我尝试了完全类似的方法,但很难让它工作。很高兴知道我走的是正确的路,尽管我猜。更多信息,请参阅和details@NeilLunn,我试过你的ap方法,但生成的语句只包含第一个id
字段:find using query:{“\u id”:{“$in”:[{“$oid”:“5f9c88676b7c1372fc31474f”}]}}}字段:Document{{logo=0}
findAllById
对我来说不是一个选项,因为我只想选择某些字段,而不是整个文档。@NeilLunn,我在查询中发现了错误:“{u id':{'$in':[?0]}”
。我必须省略括号:[]
。正确:“{'u id':{'$in':?0}”