run aggregation queries in json string format in java app

694 views
Skip to first unread message

shettysa...@gmail.com

unread,
May 24, 2018, 6:42:22 PM5/24/18
to mongodb-user
Hi 
i have an aggregation query given below which i need to pass in as a json string to java app to execute it. 

is it possible to do that . 

{'$unwind':'$results'},{'$match':{'result.studentid':'11'}}

i tried using BasicDBObject.parse but it is returning me the unwinded or flattened results and the result is not segregating on the basis of match.

is there any other way to do it.

Thanks
Sachin

Wan Bachtiar

unread,
May 25, 2018, 2:51:13 AM5/25/18
to mongodb-user

i have an aggregation query given below which i need to pass in as a json string to java app to execute it.

Hi,

Try separating the aggregation pipeline stages, and then parse using Document method.
For example:

Document unwind = Document.parse("{$unwind:'$results'}");
Document match = Document.parse("{$match:{'results.studentid':'11'}}"); 
List<Document> pipeline = asList(unwind, match);
AggregateIterable<Document> iterable = database.getCollection("collection").aggregate(pipeline);

See also MongoDB Java driver: Aggregation
Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages