Hi @Sameer_kr and welcome to MongoDB community forums!!
Based on the above requirement and the sample data examples in the $lookup documentation, I tried to create the following aggregation pipeline to sort the data based on “instock” field in descending order and the use $limit to display the max instock document.
Arrays.asList(new Document("$lookup",
new Document("from", "inventory")
.append("localField", "item")
.append("foreignField", "sku")
.append("as", "inventory_docs")),
new Document("$sort",
new Document("inventory_docs.instock", -1L)),
new Document("$limit", 1L))
If the above is not what you are looking for, could you help me with the sample document and the desired output from the aggregation pipeline.
Also, MongoDB Atlas and Compass provides you the functionality to export the aggregation pipeline stages into the desired driver code. Please visit the documentation for more information.