Click here to Skip to main content
15,887,596 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

 
Share this answer
 
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...

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