Java MongoDB Projection

I am referring mongodb official page for projection where I came across following example where elements of array in subdocument is filtered:

db.sales.aggregate([
   {
      $project: {
         items: {
            $filter: {
               input: "$items",
               as: "item",
               cond: { $gte: [ "$$item.price", 100 ] }
            }
         }
      }
   }
])

I am trying to implement this in Java but I am not doing it correctly and elements in subdocument array are not filtered.

Input Collection:

{
   _id: 0,
   items: [
     { item_id: 43, quantity: 2, price: 10 },
     { item_id: 2, quantity: 1, price: 240 }
   ]
}
{
   _id: 1,
   items: [
     { item_id: 23, quantity: 3, price: 110 },
     { item_id: 103, quantity: 4, price: 5 },
     { item_id: 38, quantity: 1, price: 300 }
   ]
}
{
    _id: 2,
    items: [
       { item_id: 4, quantity: 1, price: 23 }
    ]
}

Expected Output Collection:

{
   "_id" : 0,
   "items" : [
      { "item_id" : 2, "quantity" : 1, "price" : 240 }
   ]
}
{
   "_id" : 1,
   "items" : [
      { "item_id" : 23, "quantity" : 3, "price" : 110 },
      { "item_id" : 38, "quantity" : 1, "price" : 300 }
   ]
}
{ "_id" : 2, "items" : [ ] }

In Java(mongo Driver 3.9.1), this is what I am doing:

Bson priceFilter = Filters.gte("items.price", 100);
mongoCollection.aggregate(
  Aggregates.project(Projections.fields(priceFilter))
);

How do I project with aggregate function for the subdocument arrays where I need to filter out elements from subdocument array based on some condition?

Hello @Hamid_Jawaid,

You can build your aggregation in the MongoDB Compass using the Aggregation Pipeline Builder and then Export to Specific Language, where you have the Java option. You can use that generated Java code.

Nice! Thanks.
The example mentioned here, If I have more fields to the Document, Aggregates.project will remove them. Is there a way to leave all other fields untouched, as a document may have several other fields too. Using Projections.include(String… fieldNames) would require every field to be explicitly put there and documents may have different fields too and new fields may get added at later point in time.

Use the $addFields stage instead of $project.

Thanks Prasad. Ill check this out.