Hello everyone! I’m new to using MongoDB, and I’m trying to create an aggregation query where I need to sum some values inside an array.
Here’s what my document looks like:
documentExample = {
id: ObjectId,
name: string,
myArrayElement: {
isSuccess: boolean,
severity: string,
date: Date
}
}
Hello everyone! I’m new to using MongoDB, and I’m trying to create an aggregation query where I need to sum some values inside an array.
Here’s what my document looks like:
documentExample = {
id: ObjectId,
name: string,
myArrayElement: {
isSuccess: boolean,
severity: string,
date: Date
}[]
}
What I want to achieve is a query that filters all documents containing an element inside ‘myArrayElement’ that matches a certain date or any other conditions. I also want to get the following counts:
- The total number of items where the property ‘isSuccess’ is true.
- The total number of items where ‘isSuccess’ is false.
- The count of items where ‘severity’ is equal to ‘success’.
- The count of items where ‘severity’ is equal to ‘error’.
What is the best way to construct this query, considering that the database might contain a large amount of data? Does anyone have any insights? Thanks.