I have a mongodb which stores a collection with the following keys: parentId, childId and its own ID ("_id").
When I query the database using Spring Query, I want to get only the "_id" value and not the entire data which satisfies the Query.
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
@Autowired
private MongoTemplate mongoTemplate;
Query query = new Query(Criteria.where("parentId").is(id1).and("childId").is(id2));
List<MyClass> child = mongoTemplate.find(query, MyClass.class);
List<String> currentId = new ArrayList<>;
for (int i = 0; i < child.size(); i++) {
currentId.add(child.get(i).getId());
}
The above code gives list of Object which satisfies the Query. I then iterate through those objects and get the IDs But I want only "_id" value from those objects through the query itself.