Aggregation Queries using Java Driver

707 views
Skip to first unread message

benjami...@gmail.com

unread,
Jun 26, 2017, 6:11:24 PM6/26/17
to mongodb-user
I'd like to map the following query

db.getCollection('train-triples').aggregate(
   
[ { $match : { h : 38387 } }, { $project : { _id : 0 , t : 1, r : 1 } }, { $sample: { size: 256 } } ]
);

to the appropriate Java request. What I've tried is

        Bson filter = Aggregates.match(Filters.eq("h", headId));
       
Bson projection = Aggregates.project(Projections.fields(Projections.include("r", "t"),
               
Projections.excludeId()));
       
Bson sample = Aggregates.sample(256);
       
List<Bson> pipeline = Arrays.asList(filter, projection, sample);
       
return new CountedCursor<>(trainCollection.aggregate(pipeline).iterator(),
               
Math.min(trainCollection.count(filter), maxSize));

but this fails with
Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 2: 'unknown top level operator: $match' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "unknown top level operator: $match", "code" : 2, "codeName" : "BadValue" }
        at com
.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115)
        at com
.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114)
        at com
.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
        at com
.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
        at com
.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:176)
        at com
.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:216)
        at com
.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:207)
        at com
.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:113)
        at com
.mongodb.operation.CountOperation$1.call(CountOperation.java:236)
        at com
.mongodb.operation.CountOperation$1.call(CountOperation.java:232)
        at com
.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:422)
        at com
.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:395)
        at com
.mongodb.operation.CountOperation.execute(CountOperation.java:232)
        at com
.mongodb.operation.CountOperation.execute(CountOperation.java:55)
        at com
.mongodb.Mongo.execute(Mongo.java:836)
        at com
.mongodb.Mongo$2.execute(Mongo.java:823)
        at com
.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:188)
        at com
.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:172)


What am I doing wrong? I've tried the same pipeline with a single step instead of all 3, but get similar errors.

Jeff Yemin

unread,
Jun 27, 2017, 9:10:37 PM6/27/17
to mongodb-user
The aggregation is fine, it's the filter you're passing to count that's the problem.  It should just be the filter, not the entire $match aggregation stage.  Try this instead:


        Bson filter Filters.eq("h", headId);
        Bson match = Aggregates.match(filter);
        
Bson projection = Aggregates.project(Projections.fields(Projections.include("r", "t"),

                
Projections.excludeId()));
        
Bson sample = Aggregates.sample(256);

        
List<Bson> pipeline = Arrays.asList(match, projection, sample);

        
return new CountedCursor<>(trainCollection.aggregate(pipeline).iterator(),
                
Math.min(trainCollection.count(filter), maxSize));
 

Regards,
Jeff 
Reply all
Reply to author
Forward
0 new messages