Convert Mong-Shell Code to Java Code

327 views
Skip to first unread message

NIFRAS ISMAIL

unread,
Dec 2, 2015, 6:06:37 AM12/2/15
to mongodb-user
I am trying to convert my mongo-shell command to Java code. My mongo-shell command is here. 

    db.transactions.aggregate(
    [
        {$group:
            {   _id:
                {
                    "customer_id":"$customer_id",
                    "time_id":"$time_id"
                },
                products:
                {
                    $push:"$product_id"
                }
            }
        }
    ]);

I am trying this to convert on following way: 

            final MongoClient mongoClient = new MongoClient();
            final DB db = mongoClient.getDB("shop");
            final DBCollection collection = db.getCollection("transactions");
            
            DBObject id = new BasicDBObject("_id",new BasicDBObject("customer_id","$customer_id").append("time_id","$time_id"));

            DBObject products = new BasicDBObject("$push","$product_id");

            DBObject group = new BasicDBObject("$group",id).append("products",products);

            AggregationOutput output = collection.aggregate(group);
            System.out.println(output);

from this way I am getting error like this. 

    Exception in thread "main" com.mongodb.CommandResult$CommandFailure: command failed [aggregate]: { "serverUsed" : "/127.0.0.1:27017" , "errmsg" : "exception: A pipeline stage specification object must contain exactly one field." , "code" : 16435 , "ok" : 0.0}
at com.mongodb.CommandResult.getException(CommandResult.java:88)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:134)
at com.mongodb.DBCollection.aggregate(DBCollection.java:1311)
at com.nifrasismail.project.App.main(App.java:29)

What thing I did wrong. Do i have another way to convert mongo-shell commands to Java Code!

Wan Bachtiar

unread,
Dec 2, 2015, 10:26:33 PM12/2/15
to mongodb-user

Hi Nifras,

What MongoDB version and MongoDB Java Driver version are you using ?

In mongo-java-driver v3.0, the aggregate method accepts as its argument an array of stages. Also worth noting that if you are using mongo-java-driver v3.0 and writing a new application, try to use Document instead of DBObject. For example:

import static java.util.Arrays.asList;

MongoDatabase db = mongoClient.getDatabase("shop");
Document id = new Document("customer_id", "$customer_id").append("time_id", "$time_id");
Document products = new Document("$push", "$product_id");
Document group1 = new Document("_id", id).append("products", products );

List<Document> pipeline = asList(new Document("$group", group1));

AggregateIterable<Document> iterable = db.getCollection("transactions").aggregate(pipeline);

iterable.forEach(new Block<Document>() {
    @Override
    public void apply(final Document document) {
        System.out.println(document.toJson());
    }
});
mongoClient.close();

See Data Aggregation with Java Driver for more info.

The above snippet was tested in:

  • MongoDB v3.0.x
  • Ubuntu-14.04
  • Java 1.7
  • mongo-java-driver 3.1.0


Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages