mongodb count value of one field based on other

scrpaingnoob

I have a collection named genre_collection of following structure :

user     | genres
 ----------------
 1       |   comedy
 1       |   action
 1       |   thriller
 1       |   comedy
 1       |   action
 2       |   war
 2       |   adventure
 2       |   war
 2       |   thriller

I'm trying to find the count for each genre for each user i.e. my ideal final result would be something like this :

1 | comedy |2
1 | action |1
1 | thriller |1
2 | war |2
2 | adventure |1
2 | thriller |1

Any helps would be really useful.

felix

you can do this with aggregation using $group

try this :

db.genre_collection.aggregate([
   {
      $group:{
         _id:{
            genre:"$genres",
            user:"$user"
         },
         count:{
            $sum:1
         }
      }
   }
])

output:

{ "_id" : { "genre" : "adventure", "user" : 2 }, "count" : 1 }
{ "_id" : { "genre" : "action", "user" : 1 }, "count" : 2 }
{ "_id" : { "genre" : "thriller", "user" : 2 }, "count" : 1 }
{ "_id" : { "genre" : "war", "user" : 2 }, "count" : 2 }
{ "_id" : { "genre" : "comedy", "user" : 1 }, "count" : 2 }
{ "_id" : { "genre" : "thriller", "user" : 1 }, "count" : 1 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MongoDB Count based on output on one field

From Dev

How to do find and replace in one field based on the value in the other field?

From Dev

Find count of maximum consecutive records based on one field in Mongodb Query

From Dev

Find count of maximum consecutive records based on one field in Mongodb Query

From Dev

MongoDB - Count documents based on if a field has a particular value in group stage

From Dev

Calculated field based on sum and count of other field

From Dev

MongoDB Query SELECT one field if the other is null

From Dev

MongoDB aggregate count based on multiple query fields - (Multiple field count)

From Dev

MongoDB aggregate count based on multiple query fields - (Multiple field count)

From Dev

set field value based on other fields values

From Dev

set field value based on other fields values

From Dev

How to null values based on an other field value

From Dev

How to count based on one value in Postgres?

From Dev

How can I count field value based on another field?

From Dev

Update value of one field based on another

From Dev

Inheriting a field value based on a different one

From Dev

Grouping samples based on value dependent on one field

From Dev

Joomla Component Dynamic Custom Field based on other Field Value

From Dev

Using Collectors to GroupBy one field, Count and Add another field value

From Dev

How to Groupby one field value and Sortby another field in mongodb

From Dev

Count field values with more than one occurence in mongodb

From Dev

count rows in one table based on the id's of the other table

From Dev

count values in one pandas series based on date column and other column

From Dev

SQL Server - Case statement for one field based on content of other fields

From Dev

SSRS Count Record Based on Field Value (Status) in a Group

From Dev

Conditional validation of fields based on other field value in Symfony2

From Dev

How to update field value based on other table in mySql?

From Dev

Count based on other column

From Dev

Formula to count every cell in one column if its value is not in the other column

Related Related

  1. 1

    MongoDB Count based on output on one field

  2. 2

    How to do find and replace in one field based on the value in the other field?

  3. 3

    Find count of maximum consecutive records based on one field in Mongodb Query

  4. 4

    Find count of maximum consecutive records based on one field in Mongodb Query

  5. 5

    MongoDB - Count documents based on if a field has a particular value in group stage

  6. 6

    Calculated field based on sum and count of other field

  7. 7

    MongoDB Query SELECT one field if the other is null

  8. 8

    MongoDB aggregate count based on multiple query fields - (Multiple field count)

  9. 9

    MongoDB aggregate count based on multiple query fields - (Multiple field count)

  10. 10

    set field value based on other fields values

  11. 11

    set field value based on other fields values

  12. 12

    How to null values based on an other field value

  13. 13

    How to count based on one value in Postgres?

  14. 14

    How can I count field value based on another field?

  15. 15

    Update value of one field based on another

  16. 16

    Inheriting a field value based on a different one

  17. 17

    Grouping samples based on value dependent on one field

  18. 18

    Joomla Component Dynamic Custom Field based on other Field Value

  19. 19

    Using Collectors to GroupBy one field, Count and Add another field value

  20. 20

    How to Groupby one field value and Sortby another field in mongodb

  21. 21

    Count field values with more than one occurence in mongodb

  22. 22

    count rows in one table based on the id's of the other table

  23. 23

    count values in one pandas series based on date column and other column

  24. 24

    SQL Server - Case statement for one field based on content of other fields

  25. 25

    SSRS Count Record Based on Field Value (Status) in a Group

  26. 26

    Conditional validation of fields based on other field value in Symfony2

  27. 27

    How to update field value based on other table in mySql?

  28. 28

    Count based on other column

  29. 29

    Formula to count every cell in one column if its value is not in the other column

HotTag

Archive