[COMPASS-3600] Aggregation to Java -> Group -> eq instead of DBObject Created: 27/Apr/19  Updated: 15/May/19  Resolved: 15/May/19

Status: Closed
Project: Compass
Component/s: Aggregation pipeline, Export to Language
Affects Version/s: 1.17.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Andre Powroznik Assignee: Unassigned
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File users.json    

 Description   

Hello,

I convert an aggregation that works in Compass to Java, and it throws an exception. I have to make some very small changes to the group part to make it work (in Java, replace eq with new BasicDBObject). Is this a bug?

This aggregation, in MongoDB Compass 1.17.0:
{{[ {
 $match : {
  "source" : "Kraken",
  "currencyFrom" : "XXBT",
  "currencyTo" : "ZEUR",}}
{{ }
}, {
 $group : {
  "_id" : {
   "$toDate" : {
    "$multiply" : [ {
     "$floor" : {
      "$divide" : [

{        "$toLong" : "$date"       }

, 3600000 ]
     }
    }, 3600000 ]
   }
  },
  "ct" :

{    "$sum" : 1   }

,
  "min" :

{    "$min" : "$value"   }

,
  "avg" :

{    "$avg" : "$value"   }

,
  "max" :

{    "$max" : "$value"   }

 }
} ]}}
Converts to Java: (in Compass)

Arrays.asList(match(and(eq("source", "Kraken"), eq("currencyFrom", "XXBT"), eq("currencyTo", "ZEUR"))), group(eq("$toDate", eq("$multiply", Arrays.asList(eq("$floor", eq("$divide", Arrays.asList(eq("$toLong", "$date"), 3600000L))), 3600000L))), sum("ct", 1L), min("min", "$value"), avg("avg", "$value"), max("max", "$value")))

Which results in this exception:

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.client.model.Filters$SimpleEncodingFilter.
 at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
 at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
 at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51)
 at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:230)
 at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:148)
 at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:64)
 at org.bson.BsonDocumentWrapper.getUnwrapped(BsonDocumentWrapper.java:195)
 at org.bson.BsonDocumentWrapper.entrySet(BsonDocumentWrapper.java:165)
 at org.bson.codecs.BsonDocumentCodec.encode(BsonDocumentCodec.java:109)
 at org.bson.codecs.BsonDocumentCodec.encode(BsonDocumentCodec.java:41)
 at com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:33)
 at com.mongodb.client.model.Aggregates$GroupStage.toBsonDocument(Aggregates.java:735)
 at com.mongodb.client.internal.AggregateIterableImpl.getOutCollection(AggregateIterableImpl.java:178)
 at com.mongodb.client.internal.AggregateIterableImpl.asReadOperation(AggregateIterableImpl.java:152)
 at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132)
 at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86)
 at com.***.web.Database.getExchangeRatesAgg(Database.java:85)
 at com.***.web.Database.main(Database.java:96)

In order to fix it, in the group part, I had to replace "eq" with "new BasicDBObject".

Is this a bug in Compass?

Kind regards,



 Comments   
Comment by Massimiliano Marcon [ 09/May/19 ]

andre.powroznik@capgemini.com that's odd. I have just tried this aggregation (note the line exported from Compass):

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
 
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
 
import static com.mongodb.client.model.Accumulators.sum;
import static com.mongodb.client.model.Aggregates.group;
import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Filters.eq;
 
public class Main {
    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase database = mongoClient.getDatabase("test");
        MongoCollection collection = database.getCollection("users");
        //Line below was exported from Compass
        List stages = Arrays.asList(match(eq("email", Pattern.compile("@gmail.com$(?i)"))), group("$country", sum("usersInCountry", 1L)));
        MongoCursor cursor = collection.aggregate(stages).iterator();
        while(cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }
}

and I had no issues. Is it possible that perhaps there is something wrong with the eq you are importing?

For your reference, I also attached the collection I am running this against: users.json

Generated at Wed Feb 07 22:33:40 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.