Spring Data MongoDB Select fields to return
By:Roy.LiuLast updated:2019-08-17
In MongoDB console, you can use field:1 to select the fields to return from a query :
> db.hosting.find({},{domain:1, count:1});
In Spring Data for MongoDB, you use query.fields().include :
HostingDaoImpl.java
package com.mkyong.core.hosting.dao; import java.util.List; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Repository; import com.hostingcompass.core.db.dao.MongoDaoImpl; @Repository public class HostingDaoImpl extends MongoDaoImpl<Hosting> implements HostingDao { @Override public List<Hosting> findTopHosting(int numOfRecord) { Query query = new Query(); if (numOfRecord > 0) query.limit(numOfRecord); query.with(new Sort(Sort.Direction.DESC, "count")); query.fields().include("_id"); query.fields().include("domain"); query.fields().include("count"); return findAll(query, Hosting.class);
References
From:一号门
Previous:Spring MVC Beans loaded twice
COMMENTS