我有以下用户集合
{
"_id":"562e7c594c12942f08fe4192",
"shape":"square",
"color":"blue"
},
{
"_id":"562e7c594c12942f08fe4193",
"shape":"square",
"color":"black"
}
我怎么能得到只有_id
属性与mongodb获得id的列表
对于jpa,查询如下所示
@Query("SELECT u.id FROM User u)
Set<String> findUserIds();
如果您使用的是MongoRepository
,您可以这样写:
@Query(value="{}", fields="{_id : 1}")
List<User> findIds();
这将只填充用户对象中的id
字段,然后可以将其映射到字符串集合。