我收集了Mongo:
[{ "_id": 1, "book": "aaa", "author": "James", "seller": "Tom"},
{ "_id": 2, "book": "bbb", "author": "James", "seller": "James"},
{ "_id": 3, "book": "ccc", "author": "Mary", "seller": "Mary"},
{ "_id": 4, "book": "ddd", "author": "Mary", "seller": "James"}]
我希望按字段“_ id”获取字段值“book”的列表。
例如:[1,2] -> ["aaa", "bbb"]
如何使用Spring Data执行此操作?或者查询需要什么?
答案 0 :(得分:0)
试试这个( 未经过测试 ):
res.json()
这会返回一个@Repository
public interface YourClassNameRepository extends MongoRepository<YourClassName, String> {
@Query(value = "{'_id' : ?0}", fields = "{'book' : 1}")
List<YourClassName> loadBookNames(List<String> ids);
}
个对象列表,其中只会填充YourClassName
个字段。