mongodb group by several fields

Solutions on MaxInterview for mongodb group by several fields by the best coders in the world

showing results for - "mongodb group by several fields"
Mattia
16 Jul 2018
1db.books.aggregate([
2    { "$group": {
3        "_id": {
4            "addr": "$addr",
5            "book": "$book"
6        },
7        "bookCount": { "$sum": 1 }
8    }},
9    { "$group": {
10        "_id": "$_id.addr",
11        "books": { 
12            "$push": { 
13                "book": "$_id.book",
14                "count": "$bookCount"
15            },
16        },
17        "count": { "$sum": "$bookCount" }
18    }},
19    { "$sort": { "count": -1 } },
20    { "$limit": 2 },
21    { "$project": {
22        "books": { "$slice": [ "$books", 2 ] },
23        "count": 1
24    }}
25])
26
queries leading to this page
24group mongodb multiple fieldsgroup by multiple mongodbtwo types of group single query mongodbgroup on multiple fields mongodbmongodb 24group same multiple fields not groupinggroup by two fields mongodbmongodb query group by nested fieldgroup by mongodb return 2 object from eachmultiple group by in mongodbmongodb group and select specific fieldsgroup by multiple attributes mongodbmango group by multiple fieldsmongodb group with other field valuemongodb c 23 group by multiple fieldsgroup by 2 columns mongodbmultiple group by value mongodbmongodb aggregate two groupsmongodb group all documents with same field valuemongodb group all fields firstgroup by two columns mongodbmongodb group on multiple fieldsmongodb group by more than one fieldgroup two fields mongodbgroup by multiple columns mongodvbmongodb find and group by multiple fieldscan we use group in mongodb to twom differnt collectionsmongo 24group multiple fields not grouping correctlygroup by multiple fields mongodbmongodb aggregate group by multi elementgroup 2 mongodb requesthow to group by two fields in mongodbmongodb aggregate group id multiple fieldsmultiple group mongo aggregationmongodb group by two fieldsgroup values between two fields mongodbgroup by multiple columns mongodbmongodb group multiple fieldsmongodb group by multiple fields example 24group with two mongodbgroup by multiple things in mongo dbmongodb aggregation group with two fieldsmongodb group by multiple fields and sortmongodb group by 2 fieldsmongodb group all fieldsmongo db groupby 2 idsgroup multiple mongodb mongo db group by nested columngroup by few field and project all fields mongodbhow to group by multiple columns in mongodbmongodb group by several fieldsgroup with multiple fields mongodbmongodb group by and select multiple columns2 level group by mongodbmongodb group by multiple fields javagroup by two fields in mongodbmongodb group by two keysgroup by with 2 parameters mongodbgroup mongo by more than 1 fieldhow group documents every two fields mongodbgroup by multiple columns in mongodbmongodb group by multiple fieldsmongodb group by multiple valuesgroup based on two parameters in mongodbmongodb group on two fieldsmongodb 24group multiple fieldsgroup by query mongodb example multiplemongodb group multiple collectionsmongodb aggregate group by 2 fieldsmongodb aggregate group by multiple fieldsget group by mutiple mongodbmongodb group by multiple idmultiple group by mongodbget multiple group in mongodbgroup every two values mongodbmongodb group by nested fieldgroup by more than one column aggregate mongodb mongo shell group by two fieldsmongodb 24group multiple fields not grouping correctlymultiple group aggregate mongodbmongodb aggregate group on two fieldsgroupby multiple columns mongodbgroup by with 2 elements using mongo querygrouping by multiple fields mongodbmongodb group by example 2 collectionsmongodb group by multiple fields and return all fieldsmultiple field group by in mongodbgroup mongodb one moremongodb aggregation group by multiple fieldsmongodb aggregate group by retun 2 records from eachmongo db group items from other collectionmultiple group by in mongodb and get all fieldsmongodb group multiple fields in listgroup by more than one value mongodbmongodb group by several fields