You should read Tom Maiaroto's article "MongoDB Aggregation of all Shapes and Sizes"
It covers Map/Reduce, Grouping and The New Aggregation Framework
Question
db.users.aggregate(
{$match :
{"_id" : ObjectId("50f69176904e1d66affec20d")}
},
{$unwind : "$connections"},
{$match :
{$or : [
{"connections.users" : {$exists : false}},
{"connections.users.id" : "50f651b8b58bba7fbec2f223"}
]}},
{$group : {_id : "$connections.group"}}
)
Here I am finding the group names in which specific user exists. I am getting the correct result in mongodb shell. Now I have to use it in the lithium framework. Please help me with the command. For collection structure, see this link : http://pastebin.com/P8uMw9cj
Solution 2
You should read Tom Maiaroto's article "MongoDB Aggregation of all Shapes and Sizes"
It covers Map/Reduce, Grouping and The New Aggregation Framework
OTHER TIPS
You can run arbitrary commands like this:
$files = Files::connection()->connection->command(array("geoNear" => "fs.files", "near" => $coords, "maxDistance" => 1));
You would use Users::connection() obviously, but there are also some improvements in aggregation being worked on, though not sure if they are in the master branch.
You can also see a demo app with aggregation here: https://github.com/jmikola/li3-mongodb-aggregation-demo/blob/master/app/controllers/DemoController.php that should help you get going.