MongoDB aggregation comparison: group(), $group and MapReduce

It is somewhat confusing since the names are similar, but the group() command is a different feature and implementation from the $group pipeline operator in the Aggregation Framework. The group() command, Aggregation Framework, and MapReduce are collectively aggregation features of MongoDB. There is some overlap in features, but I’ll attempt to explain the differences and …

Read more

MongoDB – return query based on date

You probably want to make a range query, for example, all items created after a given date: db.gpsdatas.find({“createdAt” : { $gte : new ISODate(“2012-01-12T20:15:31Z”) }}); I’m using $gte (greater than or equals), because this is often used for date-only queries, where the time component is 00:00:00. If you really want to find a date that …

Read more

How can I build an $or query for MongoDB using the Java driver?

You are correct that the “default” for specifying multiple field in a query is that each field serves as a conditional filter, and thus is an AND operation. You can perform MongoDB queries with an OR clause by using the $or operand which has the following syntax : db.col.find({$or:[clause1, clause2]}) Where each clause can be …

Read more

How can I see raw mongoDB queries with Mongoid

I think I got an answer. This is follow dsims answer and also from what I’ve seen in the documentation regarding logging. I have an initialize file (config/initializers/mongoid.rb) and in there I have: Mongoid.logger = Logger.new($stdout) Mongo::Logger.logger = Logger.new($stdout) It dumps out the mongo info the console. You probably want to change this for a …

Read more

MongoDB Aggregation error : Pipeline stage specification object must contain exactly one field

MongoDB is complaining because you have an unrecognised pipeline stage specification “count”: { “$sum”: 1 } in your pipeline. Your original pipeline when formatted properly db.hashtag.aggregate([ { “$group”: { “_id”: { “year”: { “$year”: “$tweettime” }, “dayOfYear”: { “$dayOfYear”: “$tweettime” }, “interval”: { “$subtract”: [ { “$minute”: “$tweettime” }, { “$mod”: [{ “$minute”: “$tweettime”}, 15] …

Read more

Querying internal array size in MongoDB

if username Alex is unique, you can use next code: db.test.insert({username:”Alex”, tags: [‘C#’, ‘Java’, ‘C++’] }); db.test.aggregate( {$match: {username : “Alex”}}, {$unwind: “$tags”}, {$project: {count:{$add:1}}}, {$group: {_id: null, number: {$sum: “$count” }}} ); { “result” : [ { “_id” : null, “number” : 3 } ], “ok” : 1 }

Select Group by count and distinct count in same mongodb query

You are beginning to think along the right lines here as you were headed in the right direction. Changing your SQL mindset, “distinct” is really just another way of writing a $group operation in either language. That means you have two group operations happening here and, in aggregation pipeline terms, two pipeline stages. Just with …

Read more

MongoDB Projection of Nested Arrays

2017 Update Such a well put question deserves a modern response. The sort of array filtering requested can actually be done in modern MongoDB releases post 3.2 via simply $match and $project pipeline stages, much like the original plain query operation intends. db.accounts.aggregate([ { “$match”: { “email” : “john.doe@acme.com”, “groups”: { “$elemMatch”: { “name”: “group1”, …

Read more

Is there an elseif thing in MongoDB to $cond while aggregating

With modern releases ( since MongoDB 3.4 ) you would use $switch, which is basically the counterpart to switch or case keywords in other language implementations: db.items.aggregate([ { “$project”: { “name”: 1, “customfield”: { “$switch”: { “branches”: [ { “case”: { “$eq”: [ “$field1”, “4” ] }, “then”: 30 }, { “case”: { “$eq”: [ …

Read more