Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do HAVING COUNT in MongoDB?

Tags:

mongodb

My documents look like this:

{
    "_id": ObjectId("5698fcb5585b2de0120eba31"),
    "id": "26125242313",
    "parent_id": "26125241841",
    "link_id": "10024080",
    "name": "26125242313",
    "author": "gigaquack",
    "body": "blogging = creative writing",
    "subreddit_id": "6",
    "subreddit": "reddit.com",
    "score": "27",
    "created_utc": "2007-10-22 18:39:31"
}

What I'm trying to do is create a query that finds users who posted to only 1 subreddit. I did this in SQL by using the query:

Select distinct author, subreddit from reddit group by author having count(*) = 1;

I'm trying to do something similar in MongoDB but are having some troubles atm. I managed to recreate select distinct by using aggregate group but I can't figure out how to solve the HAVING COUNT part.

This is what my query looks like:

db.collection.aggregate( 
[{"$group": 
    { "_id": { author: "$author", subreddit: "$subreddit" } } },
    {$match:{count:1}} // This part is not working
])

Am I using $match wrong?

like image 939
user4742549 Avatar asked Jan 16 '16 11:01

user4742549


People also ask

What is count () in MongoDB?

The count() method counts the number of documents that match the selection criteria. It returns the number of documents that match the selection criteria. It takes two arguments first one is the selection criteria and the other is optional.

How do I group values in MongoDB?

Use the _id field in the $group pipeline stage to set the group key. See below for usage examples. In the $group stage output, the _id field is set to the group key for that document. The output documents can also contain additional fields that are set using accumulator expressions.

Can we use count with aggregate function in MongoDB?

The MongoDB $count operator allows us to pass a document to the next phase of the aggregation pipeline that contains a count of the documents. There a couple of important things to note about this syntax: First, we invoke the $count operator and then specify the string.


1 Answers

Your query should be like:

db.collection.aggregate([{
  '$group': {
    '_id': {'author': '$author', 'subreddit': '$subreddit'}, 
    'count': {'$sum': 1}, 
    'data': {'$addToSet': '$$ROOT'}}
}, {
  '$match': {
    'count': {'$eq': 1}
}}])

Where data is one-length list with matched document.

if you want to get some exact field, it should look like this:

db.collection.aggregate([{
  '$group': {
    '_id': {'author': '$author', 'subreddit': '$subreddit'}, 
    'count': {'$sum': 1}, 
    'author': {'$last': '$author'}}
}, {
  '$match': {
    'count': {'$eq': 1}
}}])
like image 153
George Petrov Avatar answered Sep 19 '22 14:09

George Petrov