(已測可以用)MongoDB Springboot2 MongoTemplate 2.13版本 動態添加查詢條件,指定返回列數據

網上很多其他方法都不能用,以下方法測試通過可以用

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");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章