Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongodb group by several fields

db.books.aggregate([
    { "$group": {
        "_id": {
            "addr": "$addr",
            "book": "$book"
        },
        "bookCount": { "$sum": 1 }
    }},
    { "$group": {
        "_id": "$_id.addr",
        "books": { 
            "$push": { 
                "book": "$_id.book",
                "count": "$bookCount"
            },
        },
        "count": { "$sum": "$bookCount" }
    }},
    { "$sort": { "count": -1 } },
    { "$limit": 2 },
    { "$project": {
        "books": { "$slice": [ "$books", 2 ] },
        "count": 1
    }}
])
Comment

PREVIOUS NEXT
Code Example
Javascript :: window.innerHeight react js 
Javascript :: JavaScript Use clearInterval() Method 
Javascript :: react native navigation navigate 
Javascript :: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html" 
Javascript :: set default date today js 
Javascript :: react native backgrunde img 
Javascript :: how to prevent event capturing in javascript 
Javascript :: javascript decode a sting in base64 
Javascript :: upload form with doc type in ajax 
Javascript :: javascript override shortcut 
Javascript :: mongodb sort objectid 
Javascript :: javascript password hashing 
Javascript :: js fetch encode url 
Javascript :: "SyntaxError: Unexpected token o in JSON at position 1 
Javascript :: angular download image base64 
Javascript :: javascript is radio button checked 
Javascript :: find positive integers javascript 
Javascript :: js arithmetic operators 
Javascript :: javascript sum digits in string of numbers 
Javascript :: set cookie and get cookie in javascript 
Javascript :: how to ssh into gke node 
Javascript :: input type number maxlength in react 
Javascript :: express js url with id 
Javascript :: Map in Javascript in LWC 
Javascript :: insertadjacenthtml javascript 
Javascript :: How to replace a value in localstorage using javascript 
Javascript :: ajax current url 
Javascript :: js list pf objects 
Javascript :: axios send post to php 
Javascript :: json data 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =