網上很多其他方法都不能用,以下方法測試通過可以用
Query query = new Query();
query.with(new Sort(Sort.Direction.DESC, "count"));
Set<String> resultFields = new HashSet<String>();
resultFields.add("name");
resultFields.add("age");
if (CollectionHelper.isNotEmpty(resultFields)) {
resultFields.forEach(f->{
query.fields().include(f);
});
}
query.addCriteria(Criteria.where("name").is("li"));
mongoTemplate.find(query, Object.class, "user");