MongoDB aggregate within daily grouping [duplicate]

In Mongo 2.8 RC2 there is a new data aggregation operator: $dateToString which can be used to group by a day and simply have a “YYYY-MM-DD” in the result: Example from the documentation: db.sales.aggregate( [ { $project: { yearMonthDay: { $dateToString: { format: “%Y-%m-%d”, date: “$date” } }, time: { $dateToString: { format: “%H:%M:%S:%L”, date: … Read more

MongoDB aggregate by field exists

I solved the same problem just last night, this way: > db.test.aggregate({$group:{_id:{$gt:[“$field”, null]}, count:{$sum:1}}}) { “_id” : true, “count” : 2 } { “_id” : false, “count” : 2 } See http://docs.mongodb.org/manual/reference/bson-types/#bson-types-comparison-order for a full explanation of how this works. Added From comment section: To check if the value doesn’t exist or is null use … Read more

MongoDB aggregate query using PHP driver

The parameter in your Javascript is an array of 4 objects with one element each, in your PHP it’s an associative array (object) with 4 elements. This would represent your Javascript: $result = $c->aggregate(array( array( ‘$project’ => array( ‘day’ => array(‘$dayOfYear’ => ‘$executed’) ), ), array( ‘$group’ => array( ‘_id’ => array(‘day’ => ‘$day’), ‘n’ … Read more

Mongodb, aggregate query with $lookup

For any particular person document, you can use the populate() function like var query = mongoose.model(“person”).find({ “name”: “foo” }).populate(“projects.tags”); And if you want to search for any persons that have any tag with ‘MongoDB’ or ‘Node JS’ for example, you can include the query option in the populate() function overload as: var query = mongoose.model(“person”).find({ … Read more