Categories
Development Java MongoDB

MongoDB: how to add the specific fields from one query with alias to result of another query using Java (with or without Spring)?

If we have collection “orders”

{"_id":"1",
 "currency": "USD"//...
}

And collection “order_items”

{"_id":"1",
 "order":"1",
 "product":"p1",
 "totalItemAmount": 25
},
 {"_id":"2",
 "order":"1",
 "product":"p2",
 "totalItemAmount": 50
}

How to get such result

[{
"orderId": "1",
"currency": "USD",
"products": ["p1", "p2"]
"totalOrderAmount": "75"
}]

using Java (with or without Spring)?

Leave a Reply

Your email address will not be published. Required fields are marked *