Hey,
I don’t believe you can use FindOne. But here is a slightly modified version that allows you to grab all information you want from the parent document and also count the votes.
> db.test01.aggregate([
... { $match: { name: "Test" } },
... { $facet: {
... "parent_fields": [
... { $project: { name: 1 } }
... ],
... "counted_votes": [
... { $unwind: "$votes" },
... { $sortByCount: "$votes.action" }
... ]
... }
... }
... ])
{ "parent_fields" : [ { "_id" : ObjectId("5fd906d828eb4534ccbceb9a"), "name" : "Test" } ], "counted_votes" : [ { "_id" : "up", "count" : 2 }, { "_id" : "down", "count" : 1 } ] }
>
I hope it helps.
All the best,
– Rodrigo