2828import com .mongodb .client .model .IndexOptions ;
2929import com .mongodb .client .result .DeleteResult ;
3030import com .mongodb .client .result .UpdateResult ;
31- import org .bson .BsonType ;
3231import org .bson .BsonValue ;
3332import org .bson .Document ;
3433import org .bson .conversions .Bson ;
3534import org .opencb .commons .datastore .core .*;
35+ import org .slf4j .Logger ;
36+ import org .slf4j .LoggerFactory ;
3637
3738import java .io .IOException ;
3839import java .util .ArrayList ;
@@ -66,6 +67,8 @@ public class MongoDBCollection {
6667 private ObjectMapper objectMapper ;
6768 private ObjectWriter objectWriter ;
6869
70+ protected Logger logger = LoggerFactory .getLogger (MongoDBCollection .class );
71+
6972 MongoDBCollection (MongoCollection <Document > dbCollection ) {
7073 this (dbCollection , null );
7174 }
@@ -129,18 +132,32 @@ public DataResult<Long> count(ClientSession clientSession, Bson query) {
129132 return endQuery (Collections .emptyList (), l , start );
130133 }
131134
132- public DataResult <String > distinct (String key , Bson query ) {
135+ public DataResult <? > distinct (String key , Bson query ) {
133136 long start = startQuery ();
134- List < String > l = new ArrayList <>();
137+ List l = new ArrayList <>();
135138 MongoCursor <BsonValue > iterator = mongoDBNativeQuery .distinct (key , query , BsonValue .class ).iterator ();
136139 while (iterator .hasNext ()) {
137140 BsonValue value = iterator .next ();
138141 if (value == null || value .isNull ()) {
139142 l .add (null );
140143 } else if (value .isString ()) {
141144 l .add (value .asString ().getValue ());
145+ } else if (value .isNumber ()) {
146+ if (value .isInt32 ()) {
147+ l .add (value .asInt32 ().getValue ());
148+ } else if (value .isInt64 ()) {
149+ l .add (value .asInt64 ().getValue ());
150+ } else {
151+ l .add (value .asDouble ().getValue ());
152+ }
153+ } else if (value .isDateTime ()) {
154+ l .add (value .asDateTime ().getValue ());
155+ } else if (value .isBoolean ()) {
156+ l .add (value .asBoolean ().getValue ());
142157 } else {
143- throw new IllegalArgumentException ("Found result with BsonType != " + BsonType .STRING + " : " + value .getBsonType ());
158+ logger .error ("Invalid field param: \" " + key + "\" . Found result with not valid BsonType: "
159+ + value .getBsonType ());
160+ throw new IllegalArgumentException ("Invalid field param: " + key );
144161 }
145162 }
146163 return endQuery (l , start );
@@ -193,7 +210,7 @@ public <T> DataResult<T> find(Bson query, ComplexTypeConverter<T, Document> conv
193210 }
194211
195212 public <T > DataResult <T > find (ClientSession clientSession , Bson query , ComplexTypeConverter <T , Document > converter ,
196- QueryOptions options ) {
213+ QueryOptions options ) {
197214 return privateFind (clientSession , query , null , null , converter , options );
198215 }
199216
@@ -202,7 +219,7 @@ public <T> DataResult<T> find(Bson query, Bson projection, ComplexTypeConverter<
202219 }
203220
204221 public <T > DataResult <T > find (ClientSession clientSession , Bson query , Bson projection , ComplexTypeConverter <T , Document > converter ,
205- QueryOptions options ) {
222+ QueryOptions options ) {
206223 return privateFind (clientSession , query , projection , null , converter , options );
207224 }
208225
@@ -219,7 +236,7 @@ public <T> List<DataResult<T>> find(List<? extends Bson> queries, Bson projectio
219236 }
220237
221238 public <T > List <DataResult <T >> find (List <? extends Bson > queries , Bson projection , ComplexTypeConverter <T , Document > converter ,
222- QueryOptions options ) {
239+ QueryOptions options ) {
223240 return privateFind (queries , projection , null , converter , options );
224241 }
225242
@@ -249,12 +266,12 @@ public <T> MongoDBIterator<T> iterator(ClientSession clientSession, List<Bson> p
249266 }
250267
251268 public <T > MongoDBIterator <T > iterator (ClientSession clientSession , Bson query , Bson projection ,
252- ComplexTypeConverter <T , Document > converter , QueryOptions options ) {
269+ ComplexTypeConverter <T , Document > converter , QueryOptions options ) {
253270 return mongoDBNativeQuery .find (clientSession , query , projection , converter , options );
254271 }
255272
256273 private <T > DataResult <T > privateFind (ClientSession clientSession , Bson query , Bson projection , Class <T > clazz ,
257- ComplexTypeConverter <T , Document > converter , QueryOptions options ) {
274+ ComplexTypeConverter <T , Document > converter , QueryOptions options ) {
258275 long start = startQuery ();
259276 MongoDBIterator <T > findIterable = iterator (clientSession , query , projection , converter , options );
260277 DataResult <T > queryResult ;
@@ -299,7 +316,7 @@ private <T> DataResult<T> privateFind(ClientSession clientSession, Bson query, B
299316 }
300317
301318 public <T > List <DataResult <T >> privateFind (List <? extends Bson > queries , Bson projection , Class <T > clazz ,
302- ComplexTypeConverter <T , Document > converter , QueryOptions options ) {
319+ ComplexTypeConverter <T , Document > converter , QueryOptions options ) {
303320 List <DataResult <T >> queryResultList = new ArrayList <>(queries .size ());
304321 for (Bson query : queries ) {
305322 DataResult <T > queryResult = privateFind (null , query , projection , clazz , converter , options );
@@ -313,7 +330,7 @@ public DataResult<Document> aggregate(List<? extends Bson> operations, QueryOpti
313330 }
314331
315332 public <T > DataResult <T > aggregate (List <? extends Bson > operations , ComplexTypeConverter <T , Document > converter ,
316- QueryOptions options ) {
333+ QueryOptions options ) {
317334
318335 long start = startQuery ();
319336
@@ -337,9 +354,9 @@ public <T> DataResult<T> aggregate(List<? extends Bson> operations, ComplexTypeC
337354// list.add(converter.convertToDataModelType(iterator.next()));
338355// }
339356// } else {
340- while (iterator .hasNext ()) {
341- list .add ((T ) iterator .next ());
342- }
357+ while (iterator .hasNext ()) {
358+ list .add ((T ) iterator .next ());
359+ }
343360// }
344361 }
345362 queryResult = endQuery (list , start );
@@ -399,7 +416,7 @@ public DataResult update(List<? extends Bson> queries, List<? extends Bson> upda
399416 }
400417
401418 public DataResult update (ClientSession clientSession , List <? extends Bson > queries , List <? extends Bson > updates ,
402- QueryOptions options ) {
419+ QueryOptions options ) {
403420 long start = startQuery ();
404421
405422 boolean upsert = false ;
@@ -459,7 +476,7 @@ public DataResult<Document> findAndUpdate(Bson query, Bson projection, Bson sort
459476 }
460477
461478 public DataResult <Document > findAndUpdate (ClientSession clientSession , Bson query , Bson projection , Bson sort , Bson update ,
462- QueryOptions options ) {
479+ QueryOptions options ) {
463480 return privateFindAndUpdate (clientSession , query , projection , sort , update , options , null , null );
464481 }
465482
@@ -468,12 +485,12 @@ public <T> DataResult<T> findAndUpdate(Bson query, Bson projection, Bson sort, B
468485 }
469486
470487 public <T > DataResult <T > findAndUpdate (ClientSession clientSession , Bson query , Bson projection , Bson sort , Bson update ,
471- Class <T > clazz , QueryOptions options ) {
488+ Class <T > clazz , QueryOptions options ) {
472489 return privateFindAndUpdate (clientSession , query , projection , sort , update , options , clazz , null );
473490 }
474491
475492 private <T > DataResult <T > privateFindAndUpdate (ClientSession clientSession , Bson query , Bson projection , Bson sort , Bson update ,
476- QueryOptions options , Class <T > clazz , ComplexTypeConverter <T , Document > converter ) {
493+ QueryOptions options , Class <T > clazz , ComplexTypeConverter <T , Document > converter ) {
477494 long start = startQuery ();
478495 Document result = mongoDBNativeQuery .findAndUpdate (clientSession , query , projection , sort , update , options );
479496 if (clazz != null && !clazz .equals (Document .class )) {
@@ -487,7 +504,7 @@ private <T> DataResult<T> privateFindAndUpdate(ClientSession clientSession, Bson
487504 }
488505
489506 public DataResult <Document > findAndModify (ClientSession clientSession , Bson query , Bson fields , Bson sort , Document update ,
490- QueryOptions options ) {
507+ QueryOptions options ) {
491508 return privateFindAndModify (clientSession , query , fields , sort , update , options , null , null );
492509 }
493510
@@ -496,7 +513,7 @@ public DataResult<Document> findAndModify(Bson query, Bson fields, Bson sort, Do
496513 }
497514
498515 public <T > DataResult <T > findAndModify (ClientSession clientSession , Bson query , Bson fields , Bson sort , Document update ,
499- QueryOptions options , Class <T > clazz ) {
516+ QueryOptions options , Class <T > clazz ) {
500517 return privateFindAndModify (clientSession , query , fields , sort , update , options , clazz , null );
501518 }
502519
@@ -505,17 +522,17 @@ public <T> DataResult<T> findAndModify(Bson query, Bson fields, Bson sort, Docum
505522 }
506523
507524 public <T > DataResult <T > findAndModify (ClientSession clientSession , Bson query , Bson fields , Bson sort , Document update ,
508- QueryOptions options , ComplexTypeConverter <T , Document > converter ) {
525+ QueryOptions options , ComplexTypeConverter <T , Document > converter ) {
509526 return privateFindAndModify (clientSession , query , fields , sort , update , options , null , converter );
510527 }
511528
512529 public <T > DataResult <T > findAndModify (Bson query , Bson fields , Bson sort , Document update , QueryOptions options ,
513- ComplexTypeConverter <T , Document > converter ) {
530+ ComplexTypeConverter <T , Document > converter ) {
514531 return privateFindAndModify (null , query , fields , sort , update , options , null , converter );
515532 }
516533
517534 private <T > DataResult <T > privateFindAndModify (ClientSession clientSession , Bson query , Bson fields , Bson sort , Document update ,
518- QueryOptions options , Class <T > clazz , ComplexTypeConverter <T , Document > converter ) {
535+ QueryOptions options , Class <T > clazz , ComplexTypeConverter <T , Document > converter ) {
519536 long start = startQuery ();
520537 Object result = mongoDBNativeQuery .findAndModify (clientSession , query , fields , sort , update , options );
521538 return endQuery (Collections .singletonList (result ), start );
0 commit comments