Click here to Skip to main content
15,077,701 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple (working) query from unwind to MongoDB, how can I write this query using Jongo? 

db.stores.aggregate([ { $unwind: "$listProducts" }, { $match : { "listProducts.productPrice" : { $ne : 0 } } }, { $group : { _id : { storeName : "$storeName", }, MAX_Products : { $max : "$listProducts.productPrice" } } } ])

What I have tried:

Aggregate.ResultsIterator max = stores.aggregate("{$unwind:{$listProducts}") .and("{$match:{ $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}


Thank you for any hint!
Posted
Updated 12-Sep-21 1:14am

1 solution

   
Comments
YustasDev13 12-Sep-21 15:39pm
   
Of course, before asking this question, I looked at https://jongo.org/
I wrote the same as there in the example "Aggregation Framework", but it does not work for me (
Richard MacCutchan 13-Sep-21 4:07am
   
What version of Mongo are you using? According to the Jongo page $unwind is only available in v2.2.
YustasDev13 13-Sep-21 6:03am
   
Good day!
4.2.0
below is the structure of my collection
e332351f3f07(mongod-4.2.0) mongoStores> db.stores.find().pretty()
{
"_id": ObjectId("613a25870e945f654c1c2c5f"),
"storeName": "Пятерочка",
"listProducts": [
{
"_id": ObjectId("613a25e10e945f654c1c2c6e"),
"productName": "Молоко",
"productPrice": 55
},
{
"_id": ObjectId("613b12710e945f654c1c2f3f"),
"productName": "Кофе",
"productPrice": 200
}
]
}
{
"_id": ObjectId("613a25bf0e945f654c1c2c67"),
"storeName": "Копейка",
"listProducts": [
{
"_id": ObjectId("613a25cc0e945f654c1c2c6a"),
"productName": "Вода",
"productPrice": 10
},
{
"_id": ObjectId("613b12660e945f654c1c2f3c"),
"productName": "Чай",
"productPrice": 100
}
]
}
Fetched 2 documents in 4ms
e332351f3f07(mongod-4.2.0) mongoStores>
e332351f3f07(mongod-4.2.0) mongoStores> db.version()
4.2.0
e332351f3f07(mongod-4.2.0) mongoStores>
Richard MacCutchan 13-Sep-21 8:29am
   
Sorry, I cannot find a single example of the use of $unwind. I suggest you try the Jong forum at Jongo - Google Groups[^].
YustasDev13 13-Sep-21 8:38am
   
thanks
and I could not find examples of using $unwind in Jongo...
Richard MacCutchan 13-Sep-21 8:46am
   
Because we have been looking in the wrong place; try https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/[^].
YustasDev13 13-Sep-21 10:30am
   
No-no)
we know how to write a request to the MongoDB shell, but we do not know how to do it in Jongo...
that's the question!
Richard MacCutchan 13-Sep-21 10:33am
   
As Jongo has no examples, and their error message is worse than useless, I think you will have to go to the Jongo forum, as I suggested above.
YustasDev13 13-Sep-21 10:50am
   
I have already posted this question on the forum, but so far no one has answered it
YustasDev13 13-Sep-21 10:32am
   
db.stores.aggregate([ { $unwind: "$listProducts" }, { $match : { "listProducts.productPrice" : { $ne : 0 } } }, { $group : { _id : { storeName : "$storeName", }, MAX_Products : { $max : "$listProducts.productPrice" } } } ])

this query works fine in MongoDB shell

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900