1

I want to parse a java string to mongo DBObject or BasicDBObject as below.

List<DBObject> query = new ArrayList<DBObject>();

String allQry = "{ \"$match\" : { \"CUSTOMERID\" : { \"$gt\" : 10}}}, { \"$project\" : { \"CUSTOMERNAME\" : 1 , \"COUNTRY\" : 1 , \"CUSTOMERID\" : 1}},{ \"$sort\" : { \"COUNTRY\" : 1}}";

BasicDBObject dbobj = BasicDBObject.parse(allQry);

query.add(dbobj);

System.out.println("qqqquery : "+query);

Cursor aggCur = collection.aggregate(query, aggOpt);

After run above example codes, it outputs qqqquery : [{ "$match" : { "CUSTOMERID" : { "$gt" : 10}}}]. There are $match , $project and $sort in allQry. Why do not it includes $project and $sort in this query? It only includes $match, who can help to check this reason? Thanks.

tkruse
  • 10,222
  • 7
  • 53
  • 80
Marika
  • 31
  • 1
  • 4
  • please format properly – Sreehari R Dec 28 '17 at 02:24
  • Not able to understand your question. Please edit it to make it more readable. – akash Dec 28 '17 at 02:28
  • 1
    You're ending the object in the middle. Replace all the `},{` with `,`. Voting to close as typo. – shmosel Dec 28 '17 at 02:49
  • Thank your help to edit example code. I replace all },{ with , it will throw excepton com.mongodb.MongoCommandException: Command failed with error 16435: 'A pipeline stage specification object must contain exactly one field.' – Marika Dec 28 '17 at 03:11
  • The error 16435 has an answer here: https://stackoverflow.com/questions/39060221/a-pipeline-stage-specification-object-must-contain-exactly-one-field-with-php-mo – tkruse Dec 28 '17 at 03:42
  • yes, i use my original allQry, it will not throw that exception. But, if I replace all },{ with , it will throw that exception. – Marika Dec 28 '17 at 06:26
  • try removing the first `}, {` or the second `}, {` only, see if it works. There seems to be a mistake in one of the second and third blocks. Your original query does not cause an error because it just ignores the second and third block. – tkruse Dec 29 '17 at 00:45
  • `BasicDBObject` parses a document not an array. Parse each stage and add them to list. – s7vr Dec 29 '17 at 00:52

1 Answers1

0

Following this tutorial: http://pingax.com/trick-convert-mongo-shell-query-equivalent-java-objects/

you could add all parts of your query like this:

MongoClient mongo = new MongoClient();
DB db = mongo.getDB("pingax");

DBCollection coll = db.getCollection("aggregationExample");

/*
 MONGO SHELL : db.aggregationExample.aggregate(
 {$match : {type : "local"}} ,
 {$project : { department : 1 , amount : 1 }}
 );
 */
 DBObject match = new BasicDBObject("$match", new BasicDBObject("type", "local")); 

 DBObject project = new BasicDBObject("$project", new BasicDBObject("department", 1).append("amount", 1));

 AggregationOutput output = coll.aggregate(match,project,group,sort);

Related:

tkruse
  • 10,222
  • 7
  • 53
  • 80
  • 1
    Thank your answer. But, this example can not resolve my issue. I need to convert all parts in java String to mongo DBObject. – Marika Dec 28 '17 at 03:40