Return mongodb 查询中只有一个属性

Return only one attribute in a mongodb query

@Query("{'recibido' : null ,'activo' : true}")
public List<Long> findEmpleadosPrlActivoRecibidoIsNull();

我希望你 return 一个包含每个对象而不是整个对象的 "employee_id" 属性的列表。

谁能帮帮我。

谢谢。

您需要在结果中指定 projection 到 select 个特定字段。

如果您只想 return employee_id,您的查询将如下所示:

db.collection.find({}, {employee_id:1, _id:0})

在上面文档中的 Java API 中,它将是:

collection.find().projection(fields(include("employee_id"), excludeId()))

创建一个 DTO,其属性名称与您希望的名称相同return

@Query("{'recibido' : null ,'activo' : true}")
public List<EmpleadoIdDTO> findIdsEmpleadosPrlActivoRecibidoIsNull();

public class EmpleadoIdDTO {
 private Long empleadoId;

 public Long getEmpleadoId() {
   return empleadoId;
 }

 public void setEmpleadoId(Long empleadoId) {
   this.empleadoId = empleadoId;
 }

}