I think I'm almost there with what I want to do but I'm falling at the last hurdle. I need to extract multiple values from a nested array along with a count.
I have many documents that looks like this:
{
"_id" : ObjectId("547db34cd9460c25e6000002"),
"doc_number" : "500715",
"error_list" : [
{
"extractor" : "Code Check",
"message_number" : "RC9999",
"message" : "Code is not synchronised"
},
{
"extractor" : "Metadata Check",
"message_number" : "RC1043",
"message" : "No metadata for document"
},
{
"extractor" : "Property Extractor",
"message_number" : "PE1012",
"message" : "No properties found"
}
]
}
I can query the collection to get a count of each error code with:
db.errors.aggregate(
[
{ $unwind : "$error_list" },
{ $group :
{ _id : "$error_list.message_number",
count: { $sum : 1 }
}
}
]
);
and return this:
{
"result" : [
{
"_id" : "PE1012",
"count" : 12
},
{
"_id" : "RC1043",
"count" : 2
},
{
"_id" : "RC9999",
"count" : 10
}
],
"ok" : 1
}
What I would like to add to the results is the message text. I can't quite work out how to do that so any help would be great.
I would like the results to look similar to this:
{
"result" : [
{
"_id" : "PE1012",
"count" : 12,
"message" : "No properties found"
},
{
"_id" : "RC1043",
"count" : 2,
"message" : "No metadata for document"
},
{
"_id" : "RC9999",
"count" : 10,
"message" : "Code is not synchronised"
}
],
"ok" : 1
}