Specify single field projection in spring mongodb query

10,790

You can simply your method by chaining the query criteria. Add the field projection to the Query class.

Add the stream to map the fields you projected.

@Override
public List<Long> find(FilterPrlDTO filterPrlDTO) {
   Criteria criteria = Criteria.where("activo").is(true).and("recibido").is(null);
   Query query = new Query(criteria);
   query.fields().include("empleadoId");
   List<String> empleadoIds = mongoOperations.find(query, Prl.class).stream().map(Prl::getEmpleadoId).collect(Collectors.toList());
   return empleadoIds;
}
Share:
10,790
Jose
Author by

Jose

Updated on August 21, 2022

Comments

  • Jose
    Jose over 1 year

    I have a query that works correctly, it returns only one attribute of my entity(Prl):

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

    Class:

    public class EmpleadoIdDTO {
    private Long empleadoId;
    
     public Long getEmpleadoId() {
       return empleadoId;
     }
    
    public void setEmpleadoId(Long empleadoId) {
    this.empleadoId = empleadoId;
     } 
    }
    

    I need to pass this Query to a Criteria because it will grow:

    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.data.mongodb.core.MongoOperations;
    import org.springframework.data.mongodb.core.mapping.Document;
    import org.springframework.data.mongodb.core.query.BasicQuery;
    import org.springframework.data.mongodb.core.query.Criteria;
    
    import com.xxx.crm482.domain.Prl;
    import com.xxxx.crm482.service.dto.FilterPrlDTO;
    
    public class PrlRepositoryImpl implements PrlRepositoryCustom {
    
    
    private final MongoOperations mongoOperations;
    
    public PrlRepositoryImpl(MongoOperations mongoOperations) {
    this.mongoOperations = mongoOperations;
    }
    
    
    @Override
    public List<Prl> find(FilterPrlDTO filterPrlDTO) {
    List<Criteria> andCriteria = new ArrayList<>();
    
    andCriteria.add(Criteria.where("activo").is(true));
    andCriteria.add(Criteria.where("recibido").is(null));
    Criteria orCriteria = new Criteria().andOperator(andCriteria.toArray(new Criteria[andCriteria.size()]));
    Document projection = new Document("empleadoId", 1);
    return mongoOperations.find(new BasicQuery(orCriteria.getCriteriaObject(), projection), Prl.class);
    }
    }
    

    And here I need that instead of returning the entity "Prl", just return the attribute "empleadoId" of the entity

  • Jose
    Jose over 6 years
    "new Document("empleadoId", 1);" <-- Cannot instantiate the type Document.
  • Jose
    Jose over 6 years
    "new BasicQuery(orCriteria.getCriteriaObject(), projection), Prl.class" <---The constructor BasicQuery(DBObject, Document) is undefined. I have updated the question so that you can see exactly, including imputations, how the code is with your instructions.
  • pvpkiran
    pvpkiran over 6 years
    Document is org.bson.Document. make sure you have the right type.