MongoDB query to filter documents and subdocuments

{
  A: "number",
  B: [{C: "number"}, etc,]
}

The above is the schema of my documents.

How to write an aggregation pipeline to find all documents where:

  • A > 100
  • And then filters the subdocuments of B to return only the one where C is the maximum.

Hello @Big_Cat_Public_Safety_Act what is the “maximum” of C ?

Assuming you want that to be greater than 100 as well, you can use a query like this (you don’t need aggregation proper)…

db.question.find(
  { "A": { $gt: 100 } },
  { "B": { "$elemMatch": { "C": { $gt: 100 } } } }
)
[
  { _id: ObjectId("63a6180ee1fc395f0e1e131a"), B: [ { C: 101 } ] }
]