{"id":42449,"date":"2016-12-19T11:00:42","date_gmt":"2016-12-19T09:00:42","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=42449"},"modified":"2016-12-17T12:26:29","modified_gmt":"2016-12-17T10:26:29","slug":"mongodb-aggregate-count-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/","title":{"rendered":"MongoDb Aggregate Count Example"},"content":{"rendered":"<p>Today we will learn about MongoDb\u2019s aggregate function \u2018Count,\u2019 but first let\u2019s look at a brief overview of aggregate functions. In SQL or other relational databases, aggregation in the process that records and returns computed results like SUM MIN, MAX, AVG, and COUNT. MongoDb uses a similar concept but in a different fashion &#8211; it uses the concept of data processing pipelines.<\/p>\n<p>Documents enter a multi-stage pipeline which transforms them into an aggregated result. Here, \u2018Document\u2019 refers to a record, so don\u2019t confuse it if you are new to MongoDb; I will explain when needed.<\/p>\n<h2>1. Aggregate Concept<\/h2>\n<p>There are several stages in Data Aggregation like filtering, Sorting, grouping etc. , documents transformation that modify the form of the<\/p>\n<h3>1.1 Count() Aggregate function<\/h3>\n<p><span style=\"font-weight: 400;\">Count is an aggregate function used to count the number of documents, documents per group, or perform a Condition based count. It is is the same as COUNT in SQL or similar languages. Let\u2019s see some examples:<\/span><\/p>\n<p>First, we need to perform some basic operations, like create a database, collection, and then insertion of some documents to perform the Count query.<\/p>\n<h3>1.2 Create Database<\/h3>\n<p>The first step is to create a database. MongoDb uses DATABASE_NAME to create a database. The command will create a new database if it doesn\u2019t exist, otherwise it will return the existing one with the name used. So \u2018use\u2019 is the Keyword, and \u2018DATABASE_NAME\u2019 can be any name.<\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s see how many databases there are in the system.<\/span><\/p>\n<p>Note: I\u2019m using Compass MongoDb GUI &#8211; you can download it from the MongoDb site. I will also show you the shell command to enable a better understanding.<\/p>\n<p><span style=\"font-weight: 400;\">In Compass, you can see the database listed.<\/span><\/p>\n<p><figure id=\"attachment_42509\" aria-describedby=\"caption-attachment-42509\" style=\"width: 680px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig1.jpg\"><img decoding=\"async\" class=\"wp-image-42509 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig1-e1481639573978.jpg\" width=\"680\" height=\"336\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig1-e1481639573978.jpg 680w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig1-e1481639573978-300x148.jpg 300w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/a><figcaption id=\"caption-attachment-42509\" class=\"wp-caption-text\">CompassView1<\/figcaption><\/figure><\/p>\n<p>There are 3 Databases and 4 collections showing above. Let\u2019s create a new Database.\u00a0In Shell, we can see the listed database.<br \/>\nLet\u2019s create a Database using the shell<\/p>\n<p><figure id=\"attachment_42510\" aria-describedby=\"caption-attachment-42510\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42510\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig2.jpg\" alt=\"\" width=\"602\" height=\"302\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig2.jpg 602w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig2-300x150.jpg 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-42510\" class=\"wp-caption-text\">ShellView1.1<\/figcaption><\/figure><\/p>\n<p>&nbsp;<\/p>\n<p><figure id=\"attachment_42514\" aria-describedby=\"caption-attachment-42514\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig3.jpg\"><img decoding=\"async\" class=\"wp-image-42514 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig3-e1481639492398.jpg\" width=\"640\" height=\"191\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig3-e1481639492398.jpg 640w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig3-e1481639492398-300x90.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-42514\" class=\"wp-caption-text\">CreateDatabase1.3<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Above we showed that database myFirstDataBase has been created but it&#8217;s not showing database in shell. The reason is that we have not inserted any records because it&#8217;s an empty database. Lets insert a record but before that let\u2019s see what Compass is showing.<\/p>\n<p><figure id=\"attachment_42516\" aria-describedby=\"caption-attachment-42516\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig4-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42516\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig4-1.jpg\" alt=\"\" width=\"602\" height=\"172\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig4-1.jpg 602w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig4-1-300x86.jpg 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-42516\" class=\"wp-caption-text\">CompassDatabase1.4<\/figcaption><\/figure><\/p>\n<p>It\u2019s the same &#8211; there\u2019s no difference between the shell and Compass. Now insert a document (record) into the database. Do it from the shell, and we will see it in Compass too.<\/p>\n<p><figure id=\"attachment_42517\" aria-describedby=\"caption-attachment-42517\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig5.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42517\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig5.jpg\" alt=\"\" width=\"602\" height=\"263\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig5.jpg 602w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig5-300x131.jpg 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-42517\" class=\"wp-caption-text\">InsertDocument1.5<\/figcaption><\/figure><\/p>\n<p>db.myFirstCol.insert({\u201ccode\u201d:001})<\/p>\n<p>Where:<\/p>\n<p>db is the current database<br \/>\nmyFirstCol is the Collection name which is automatically created<br \/>\ninsert is the MongoDb keyword to insert document<br \/>\ncode is a field\/attribute<br \/>\n001 is the value of code<\/p>\n<p>I inserted some documents into the database using myFirstCol.<\/p>\n<p><figure id=\"attachment_42518\" aria-describedby=\"caption-attachment-42518\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig6.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42518\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig6.jpg\" alt=\"\" width=\"602\" height=\"365\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig6.jpg 602w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig6-300x182.jpg 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-42518\" class=\"wp-caption-text\">CompassViewDocuments1.6<\/figcaption><\/figure><\/p>\n<h3>1.3 Documents Count<\/h3>\n<p>db.collection.count(query, options)<br \/>\nWhere \u2018query\u2019 is the Selection criteria and \u2018options\u2019 is an extra option for modifying count.<\/p>\n<p><figure id=\"attachment_42519\" aria-describedby=\"caption-attachment-42519\" style=\"width: 800px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig7.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42519\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig7.jpg\" alt=\"\" width=\"800\" height=\"553\" \/><\/a><figcaption id=\"caption-attachment-42519\" class=\"wp-caption-text\">ShowDatabase1.7<\/figcaption><\/figure><\/p>\n<p>There is another construct<\/p>\n<p>db.collection.find(query).count()<\/p>\n<p><figure id=\"attachment_42520\" aria-describedby=\"caption-attachment-42520\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig8.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42520\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig8.jpg\" alt=\"\" width=\"602\" height=\"104\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig8.jpg 602w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig8-300x52.jpg 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-42520\" class=\"wp-caption-text\">ShowDatabase1.8<\/figcaption><\/figure><\/p>\n<p>The difference here is that \u2018find\u2019 will find all documents and then count, whereas db.collection.count() will just count the documents.<\/p>\n<p>To clarify: \u2018find()\u2019 selects documents in a collection that match the query criteria, and returns a cursor to the selected documents . When the find() method \u201creturns documents,\u201d it is actually returning a cursor to the documents. In contrast, db.collection.count() performs the query and counts the documents.<\/p>\n<p>Whereas, db.collection.count() just do the query and count the documents.<\/p>\n<p>Note: Sometimes in a shared cluster environment, db.collection.count() may give you an inaccurate result due to invalid documents being present during abnormal migration or shutdown. This is outside the scope of this document, but we may discuss in in another article. Returning to db.collection.count(query, options) &#8211; query is the condition, and options are extra methods modify the count result.<\/p>\n<p><figure id=\"attachment_42521\" aria-describedby=\"caption-attachment-42521\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig9.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42521\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig9.jpg\" alt=\"\" width=\"602\" height=\"295\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig9.jpg 602w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig9-300x147.jpg 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-42521\" class=\"wp-caption-text\">ShowDocuments1.9<\/figcaption><\/figure><\/p>\n<p>Above shows count operation on collection \u201cWhere Grade = \u201cB\u201d\u201d, then shows skip(1) and limit(1) optional document attributes.<br \/>\nThe Skip process is -&gt; Condition(Where Grade = \u2018B\u2019) -&gt; Skip n documents -&gt; Count, and the same for Limit. We can use multiple conditions to get the required result.<\/p>\n<h3>1.4 Cursor Count<\/h3>\n<p>As explained earlier, count the documents in the cursor by using db.collection.find(query).count(), count is appended in find(). The operation does not perform the query but instead counts the number of results that would be returned by the query.<\/p>\n<p><figure id=\"attachment_42522\" aria-describedby=\"caption-attachment-42522\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig10.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42522\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig10.jpg\" alt=\"\" width=\"602\" height=\"235\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig10.jpg 602w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig10-300x117.jpg 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-42522\" class=\"wp-caption-text\">ShowDocuments1.10<\/figcaption><\/figure><\/p>\n<h3>1.5 Count using Aggregate function<\/h3>\n<p>There are more powerful ways to do count, lets see how aggregate helps us<\/p>\n<p><figure id=\"attachment_42523\" aria-describedby=\"caption-attachment-42523\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig11.jpg\"><img decoding=\"async\" class=\"size-full wp-image-42523\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig11.jpg\" alt=\"\" width=\"602\" height=\"237\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig11.jpg 602w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/12\/fig11-300x118.jpg 300w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/a><figcaption id=\"caption-attachment-42523\" class=\"wp-caption-text\">ShowDocuments1.11<\/figcaption><\/figure><\/p>\n<p>Aggregate is a pipeline, $group is stage. Above query is group by grade. You can do many things like create a condition using $match stage OR if you want to make a complicated query like $Group -&gt; $Match -&gt; $Sort -&gt; $Group and then summarize your result using $Project etc. Its totally dependent on the complexity of your requirements but here it\u2019s out of scope.<\/p>\n<h2>2. Summary<\/h2>\n<p>Count gives an idea of how it works in MongoDb. We covered two type of count construct: One is db.collection.count() and db.collection.find().count(). We also showed how to do grouping and then count, and a little about aggregate function and its usage. This is just a brief about explanation of count in MongoDb.<\/p>\n<p><em>This article has been proofread by Mark Adams<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we will learn about MongoDb\u2019s aggregate function \u2018Count,\u2019 but first let\u2019s look at a brief overview of aggregate functions. In SQL or other relational databases, aggregation in the process that records and returns computed results like SUM MIN, MAX, AVG, and COUNT. MongoDb uses a similar concept but in a different fashion &#8211; it &hellip;<\/p>\n","protected":false},"author":104,"featured_media":36154,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1447],"tags":[],"class_list":["post-42449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MongoDb Aggregate Count Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Today we will learn about MongoDb\u2019s aggregate function \u2018Count,\u2019 but first let\u2019s look at a brief overview of aggregate functions. In SQL or other\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDb Aggregate Count Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Today we will learn about MongoDb\u2019s aggregate function \u2018Count,\u2019 but first let\u2019s look at a brief overview of aggregate functions. In SQL or other\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-12-19T09:00:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Adam Aijaz\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Adam Aijaz\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/\"},\"author\":{\"name\":\"Adam Aijaz\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/75fcd88a5fd2ad2d5970a9ef8e58e475\"},\"headline\":\"MongoDb Aggregate Count Example\",\"datePublished\":\"2016-12-19T09:00:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/\"},\"wordCount\":960,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"articleSection\":[\"MongoDB\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/\",\"name\":\"MongoDb Aggregate Count Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"datePublished\":\"2016-12-19T09:00:42+00:00\",\"description\":\"Today we will learn about MongoDb\u2019s aggregate function \u2018Count,\u2019 but first let\u2019s look at a brief overview of aggregate functions. In SQL or other\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Software Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MongoDB\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/software-development\/mongodb\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"MongoDb Aggregate Count Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/75fcd88a5fd2ad2d5970a9ef8e58e475\",\"name\":\"Adam Aijaz\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Adam-Aijaz-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Adam-Aijaz-96x96.jpg\",\"caption\":\"Adam Aijaz\"},\"description\":\"Adam got initial professional qualification from RMIT University Melbourne Australia, where he got Master in IT and Business, he got further Professional Certification in IT (CP) from Australian Computer Society (ACS) and Diploma in IT and Finance from Australis College, Brisbane. He got more than 15 years of IT experience in Software development on different technologies, he works mostly on Java, spring, JavaScript, Front end like Angular, JSF, Vaadin etc. His interest in Integration of software\u2019s, SOA and process re-engineering. He runs his own IT consultancy and provided all sort of IT solutions to SME (Small to Medium Enterprises).\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\",\"https:\/\/www.linkedin.com\/in\/adam-aijaz-certified-it-professional-cp-macs-dfn-44b44224\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/adam-aijaz\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDb Aggregate Count Example - Java Code Geeks","description":"Today we will learn about MongoDb\u2019s aggregate function \u2018Count,\u2019 but first let\u2019s look at a brief overview of aggregate functions. In SQL or other","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/","og_locale":"en_US","og_type":"article","og_title":"MongoDb Aggregate Count Example - Java Code Geeks","og_description":"Today we will learn about MongoDb\u2019s aggregate function \u2018Count,\u2019 but first let\u2019s look at a brief overview of aggregate functions. In SQL or other","og_url":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-12-19T09:00:42+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","type":"image\/jpeg"}],"author":"Adam Aijaz","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Adam Aijaz","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/"},"author":{"name":"Adam Aijaz","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/75fcd88a5fd2ad2d5970a9ef8e58e475"},"headline":"MongoDb Aggregate Count Example","datePublished":"2016-12-19T09:00:42+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/"},"wordCount":960,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","articleSection":["MongoDB"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/","url":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/","name":"MongoDb Aggregate Count Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","datePublished":"2016-12-19T09:00:42+00:00","description":"Today we will learn about MongoDb\u2019s aggregate function \u2018Count,\u2019 but first let\u2019s look at a brief overview of aggregate functions. In SQL or other","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/mongodb-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/software-development\/mongodb\/mongodb-aggregate-count-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Software Development","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/"},{"@type":"ListItem","position":3,"name":"MongoDB","item":"https:\/\/examples.javacodegeeks.com\/category\/software-development\/mongodb\/"},{"@type":"ListItem","position":4,"name":"MongoDb Aggregate Count Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/75fcd88a5fd2ad2d5970a9ef8e58e475","name":"Adam Aijaz","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Adam-Aijaz-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/10\/Adam-Aijaz-96x96.jpg","caption":"Adam Aijaz"},"description":"Adam got initial professional qualification from RMIT University Melbourne Australia, where he got Master in IT and Business, he got further Professional Certification in IT (CP) from Australian Computer Society (ACS) and Diploma in IT and Finance from Australis College, Brisbane. He got more than 15 years of IT experience in Software development on different technologies, he works mostly on Java, spring, JavaScript, Front end like Angular, JSF, Vaadin etc. His interest in Integration of software\u2019s, SOA and process re-engineering. He runs his own IT consultancy and provided all sort of IT solutions to SME (Small to Medium Enterprises).","sameAs":["https:\/\/www.javacodegeeks.com","https:\/\/www.linkedin.com\/in\/adam-aijaz-certified-it-professional-cp-macs-dfn-44b44224"],"url":"https:\/\/examples.javacodegeeks.com\/author\/adam-aijaz\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/42449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=42449"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/42449\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/36154"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=42449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=42449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=42449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}