|
24 | 24 | import lombok.RequiredArgsConstructor;
|
25 | 25 |
|
26 | 26 | import java.io.IOException;
|
27 |
| -import java.util.ArrayList; |
28 |
| -import java.util.Collection; |
29 |
| -import java.util.Collections; |
30 |
| -import java.util.HashMap; |
31 |
| -import java.util.HashSet; |
32 |
| -import java.util.Iterator; |
33 |
| -import java.util.LinkedHashSet; |
34 |
| -import java.util.List; |
35 |
| -import java.util.Map; |
| 27 | +import java.util.*; |
36 | 28 | import java.util.Map.Entry;
|
37 |
| -import java.util.Optional; |
38 |
| -import java.util.Scanner; |
39 |
| -import java.util.Set; |
40 | 29 | import java.util.concurrent.TimeUnit;
|
41 | 30 |
|
42 | 31 | import org.bson.Document;
|
|
127 | 116 | import org.springframework.util.ResourceUtils;
|
128 | 117 | import org.springframework.util.StringUtils;
|
129 | 118 |
|
130 |
| -import com.mongodb.Cursor; |
131 |
| -import com.mongodb.DBCollection; |
132 |
| -import com.mongodb.DBCursor; |
133 |
| -import com.mongodb.Mongo; |
134 | 119 | import com.mongodb.MongoClient;
|
135 | 120 | import com.mongodb.MongoException;
|
136 | 121 | import com.mongodb.ReadPreference;
|
137 | 122 | import com.mongodb.WriteConcern;
|
138 | 123 | import com.mongodb.client.AggregateIterable;
|
| 124 | +import com.mongodb.client.DistinctIterable; |
139 | 125 | import com.mongodb.client.FindIterable;
|
140 | 126 | import com.mongodb.client.MapReduceIterable;
|
141 | 127 | import com.mongodb.client.MongoCollection;
|
|
173 | 159 | * @author Laszlo Csontos
|
174 | 160 | * @author Maninder Singh
|
175 | 161 | * @author Borislav Rangelov
|
| 162 | + * @author duozhilin |
176 | 163 | */
|
177 | 164 | @SuppressWarnings("deprecation")
|
178 | 165 | public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider {
|
@@ -808,6 +795,30 @@ public <T> T findById(Object id, Class<T> entityClass, String collectionName) {
|
808 | 795 | return doFindOne(collectionName, new Document(idKey, id), new Document(), entityClass);
|
809 | 796 | }
|
810 | 797 |
|
| 798 | + public <T, Z> List<T> distinct(String field, Class<Z> entityClass, Class<T> resultClass) { |
| 799 | + return distinct(new Query(), field, determineCollectionName(entityClass), resultClass); |
| 800 | + } |
| 801 | + |
| 802 | + public <T, Z> List<T> distinct(Query query, String field, Class<Z> entityClass, Class<T> resultClass) { |
| 803 | + return distinct(query, field, determineCollectionName(entityClass), resultClass); |
| 804 | + } |
| 805 | + |
| 806 | + public <T> List<T> distinct(Query query, String field, String collectionName, Class<T> resultClass) { |
| 807 | + MongoCollection<Document> collection = this.getCollection(collectionName); |
| 808 | + DistinctIterable<T> iterable = collection.distinct(field, query.getQueryObject(), resultClass); |
| 809 | + |
| 810 | + MongoCursor<T> cursor = iterable.iterator(); |
| 811 | + |
| 812 | + List<T> result = new ArrayList<T>(); |
| 813 | + |
| 814 | + while (cursor.hasNext()) { |
| 815 | + T object = cursor.next(); |
| 816 | + result.add(object); |
| 817 | + } |
| 818 | + |
| 819 | + return result; |
| 820 | + } |
| 821 | + |
811 | 822 | @Override
|
812 | 823 | public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass) {
|
813 | 824 | return geoNear(near, entityClass, determineCollectionName(entityClass));
|
|
0 commit comments