Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to do a HAVING in a MongoDB GROUP BY?

For what would be this query in SQL (to find duplicates):

SELECT userId, name FROM col GROUP BY userId, name HAVING COUNT(*)>1 

I performed this simple query in MongoDB:

res = db.col.group({key:{userId:true,name:true},                       reduce: function(obj,prev) {prev.count++;},                       initial: {count:0}}) 

I've added a simple Javascript loop to go over the result set, and performed a filter to find all the fields with a count > 1 there, like so:

for (i in res) {if (res[i].count>1) printjson(res[i])}; 

Is there a better way to do this other than using javascript code in the client? If this is the best/simplest way, say that it is, and this question will help someone :)

like image 482
shlomoid Avatar asked Apr 05 '11 10:04

shlomoid


People also ask

How do I write a group condition in MongoDB?

The use of the $cond operator here determines whether the "status" is actually a defect or not and the result is a conditional $sum where only the "defect" values are counted. Once those are grouped per day you simply $divide the result, with another check with $cond to make sure you are not dividing by zero.

Which command is correct for grouping a set of documents by the field?

The $group stage separates documents into groups according to a "group key". The output is one document for each unique group key.

Can we do group by in MongoDB?

MongoDB group by is used to group data from the collection, we can achieve group by clause using aggregate function and group method in MongoDB. While using aggregate function with group by clause query operations is faster as normal query, basically aggregate function is used in multiple condition.

What is $$ root in MongoDB?

The $$ROOT variable contains the source documents for the group. If you'd like to just pass them through unmodified, you can do this by $pushing $$ROOT into the output from the group.


1 Answers

New answer using Mongo aggregation framework

After this question was asked and answered, 10gen released Mongodb version 2.2 with an aggregation framework. The new best way to do this query is:

db.col.aggregate( [    { $group: { _id: { userId: "$userId", name: "$name" },                count: { $sum: 1 } } },    { $match: { count: { $gt: 1 } } },    { $project: { _id: 0,                   userId: "$_id.userId",                   name: "$_id.name",                   count: 1}} ] ) 

10gen has a handy SQL to Mongo Aggregation conversion chart worth bookmarking.

like image 126
Old Pro Avatar answered Sep 19 '22 12:09

Old Pro