2017-10-16 79 views
1

雖然這部作品在蒙戈:與geoQuery不同的值工作在蒙戈,但不是在春天

> db.collection.distinct("key", {"position":{$geoWithin:{$box: [[-6.8, 36.7], [-0.5, 43.8]]}}}) 
> [ "A", "B" ] 

在春天返回一個錯誤

Criteria criteria = Criteria.where("position").within(box); 
     Query query = new Query(); 
     query.addCriteria(criteria); 
List<String> result = mongoOperations.getCollection("collection") 
       .distinct("key", query.getQueryObject()); 

java.lang.IllegalArgumentException異常:不能序列類org.springframework.data.mongodb.core.query.GeoCommand at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:299)〜[mongo-java-driver-2.14.3.jar:na] at org.bson .BasicBSONEncoder.putObject(BasicBSONEncoder.java:194)〜[mongo-jav a-driver-2.14.3.jar:na] at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:255)〜[mongo-java-driver-2.14.3.jar:na] at org.bson。 BasicBSONEncoder.putObject(BasicBSONEncoder.java:194)〜[mongo-java-driver-2.14.3.jar:na] at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:255)〜[mongo-java-driver- 2.14.3.jar:na] at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:194)〜[mongo-java-driver-2.14.3.jar:na] at org.bson.BasicBSONEncoder.putObject(基本BSONEncoder.java:136)〜[mongo-java-driver-2.14.3.jar:na] at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:36)〜[mongo-java-driver-2.14.3。 jar:na] at com.mongodb.OutMessage.putObject(OutMessage.java:289)〜[mongo-java-driver-2.14.3.jar:na] 在com.mongodb.OutMessage.writeQuery(OutMessage.java:211)〜[mongo-java-driver-2.14.3.jar:na] at com.mongodb.OutMessage.query(OutMessage.java:86)〜[mongo -java-driver-2.14.3.jar:na]

回答

0

我無法使@Christoph Strobl代碼甚至編譯!

我終於用蒙戈-Java的應用程序,以獲得不同的值:

MongoClient mongoClient = new MongoClient(); 
    MongoDatabase database = mongoClient.getDatabase(dbName); 
    MongoCollection<org.bson.Document> collection = database.getCollection(COLLECTION); 
    Bson query = Filters.geoWithinBox("position", 
      x1, y1, x2, y2); 

    DistinctIterable<String> resultList = collection.distinct("key", query, String.class); 

    Set resultSet = new HashSet(); 
    resultList.forEach(new Block<String>() { 
     @Override 
     public void apply(final String result) { 
      resultSet.add(result); 
     } 
    }); 

    return resultSet; 
0

我確實看到需要通過集合的操作直接通過MongoDB Java驅動程序API。但Query.getQueryObject()不能直接與底層驅動程序一起使用,因爲它由QueryMapper轉換。 現在,您可以嘗試自行完成映射。

QueryMapper mapper = new QueryMapper(mongoOperations.getConverter()); 
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(), Optional.empty()); 

List<String> result = template.getCollection("collection") 
    .distinct("key", mappedQuery, String.class) 
    .into(new ArrayList<>()); 

還有DATAMONGO-1761(韌仍處於打開狀態)尋求爲distinct支持添加到MongoOperations