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

Create filter aggregation in spring

You can try below query. Static Imports import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter; import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf; Code Aggregation aggregation = newAggregation( project().and(filter(“parts”) .as(“item”) .by(valueOf( “item.currentState”) .equalToValue( “Estimation Confirmed”))) .as(“parts”); ); List<outputType> results = mongoTemplate.aggregate(aggregation, inputType, outputType)

MongoDB not equal to

Use $ne — $not should be followed by the standard operator: An examples for $ne, which stands for not equal: use test switched to db test db.test.insert({author : ‘me’, post: “”}) db.test.insert({author : ‘you’, post: “how to query”}) db.test.find({‘post’: {$ne : “”}}) { “_id” : ObjectId(“4f68b1a7768972d396fe2268”), “author” : “you”, “post” : “how to query” } … 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