Jose
Jose

Reputation: 1829

Return only one attribute in a mongodb query

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

I want you to return a list with the "employee_id" attribute of each object and not the whole object.

Can somebody help me.

Thank you.

Upvotes: 0

Views: 1542

Answers (2)

Jose
Jose

Reputation: 1829

Create a DTO that has an attribute with the same name that you want it to 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;
 }

}

Upvotes: 0

Atish
Atish

Reputation: 4435

You need to specify projection to select specific fields in result.

If you want to return only employee_id, your query will look like this:

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

In Java API sepcified in doc above it will be:

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

Upvotes: 1

Related Questions