V
V
Vitaly Gusev2021-09-12 11:29:49
MongoDB
Vitaly Gusev, 2021-09-12 11:29:49

Query in MongoDB using Jongo?

I have a MongoDB query that works:
db.stores.aggregate([ { $unwind: "$listProducts" }, { $match : { "listProducts.productPrice" : { $ne : 0 } } }, { $group : { _id : { storeName : "$storeName", }, MAX_Products : { $max : "$listProducts.productPrice" } } } ])

how can I write the same query using Jongo?
i tried like this:
Aggregate.ResultsIterator max = stores.aggregate("{$unwind:{$listProducts}") .and("{$match:{$listProducts.productPrice $ne : 0 }") .and("{ $group:{$storeName}") .and("{$max:{$listProducts.productPrice}") .as(Store.class);

But I get:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot parse query: {$unwind:{$listProducts}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Philipp, 2021-09-16
@zoonman

You have syntax errors. Fix it and everything will work.

V
Vitaly Gusev, 2021-09-17
@Yustas13

So that was the question - how to write this syntactically correctly using Jongo.
Unfortunately Jongo's documentation is very short, and I didn't find any example with unwind, I wrote dozens of query options, but I couldn't "guess" the correct syntax, so I turned to the forum, in the hope that someone could know it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question