w3resource

MongoDB count function - cursor method

Description

In this page, we are going to discuss the usage of count function in mongodb. The count() function returns the number of documents in a specified collection.

Our database name is 'myinfo' and our collection name is 'testtable'. Here, is the collection bellow.

Sample collection "testtable"

{
        "_id" : ObjectId("528f4e630fe5e6467e58ae7b"),
        "user_id" : "user1",
        "password" : "1a2b3c",
        "sex" : "Male",
        "age" : 17,
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "CONSULTANT",
        "interest" : "MUSIC",
        "extra" : {
                "community_name" : [
                        "MODERN MUSIC",
                        "CLASSICAL MUSIC",
                        "WESTERN MUSIC"
                ],
                "community_moder_id" : [
                        "MR. Alex",
                        "MR.   Dang",
                        "MR Haris"
                ],
                "community_members" : [
                        700,
                        200,
                        1500
                ],
                "friends" : {
                        "valued_friends_id" : [
                                "kumar",
                                "harry",
                                "anand"
                        ],
                        "ban_friends_id" : [
                                "Amir",
                                "Raja",
                                "mont"
                        ]
                }
        }
}
{
        "_id" : ObjectId("528f4e720fe5e6467e58ae7c"),
        "user_id" : "user2",
        "password" : "11aa1a",
        "sex" : "Male",
        "age" : 24,
        "date_of_join" : "17/10/2009",
        "education" : "M.B.A.",
        "profession" : "MARKETING",
        "interest" : " MUSIC",
        "extra" : {
                "community_name" : [
                        "MODERN MUSIC",
                        "CLASSICAL MUSIC",
                        "WESTERN MUSIC"
                ],
                "co mmunity_moder_id" : [
                        "MR. Roy",
                        "MR. Das",
                        "MR Doglus"
                ],
                "community_members" : [
                        500,
                        300,
                        1400
                ],
                "friends" : {
                        "valued_friends_id" : [
                                "pal",
                                "viki",
                                "john"
                        ],
                        "ban_friends_id" : [
                                "jalan",
                                "mono j",
                                "evan"
                        ]
                }
        }
}
{
        "_id" : ObjectId("528f4e7e0fe5e6467e58ae7d"),
        "user_id" : "user3",
        "password" : "b1c1d1",
        "sex" : "Female",
        "age" : 19,
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "IT COR.",
        "interest" : "AR T",
        "extra" : {
                "community_name" : [
                        "MODERN ART",
                        "CLASSICAL ART",
                        "WESTERN ART"
                ],
                "community_mo der_id" : [
                        "MR. Rifel",
                        "MR. Sarma",
                        "MR Bhatia"
                ],
                "community_members" : [
                        5000,
                        2000,
                        1500
                ],
                "friends" : {
                        "valued_friends_id" : [
                                "philip",
                                "anant",
                                "alan"
                        ],
                        "ban_friends_id" : [
                                "Amir",
                                "Raja",
                                "mont"
                        ]
                }
        }
}
{
        "_id" : ObjectId("528f4e910fe5e6467e58ae7e"),
        "user_id" : "user4",
        "password" : "abczyx",
        "sex" : "Female",
        "age" : 22,
        "date_of_join" : "17/8/2009",

        "education" : "M.B.B.S.",
        "profession" : "DOCTOR",
        "interest" : "SPORTS",
        "extra" : {
                "community_name" : [
                        "ATHELATIC",
                        "GAMES FAN GYES",
                        "FAVOURIT GAMES"
                ],
                "community_moder_id" : [
                        "MR. Paul",
                        "MR. Das",
                        "MR Doglus"
                ],
                "community_members" : [
                        2500,
                        2200,
                        3500
                ],
                "friends" : {
                        "valued_friends_id" : [
                                "vinod",
                                "viki",
                                "john"
                        ],
                        "ban_friends_id" : [
                                "jalan",
                                "monoj",
                                "evan"
                        ]
                }
        }
}
{
        "_id" : ObjectId("528f4f8a0fe5e6467e58ae7f"),
        "user_id" : "user5",
        "password" : "user5",
        "sex" : "Male",
        "age" : 21,
        "date_of_join" : "17/08/2011",
        "education" : "MCA",
        "profession" : "S.W. Engineer",
        "interest" : "SPORTS",
        "extra" : {
                "community_name" : [
                        "ATHELATIC",
                        "GAMES FAN GYES",
                        "FAVOURIT GAMES"
                ]
        }
}

Document written in command prompt.

MongoDB count() example

If we want to get the number of documents in the collection "testtable", the following mongodb command can be used :

>db.testtable.count()

Output of the command:

> db.testtable.count();
5

MongoDB count() example with condition

If we want to get the number of documents in the collection "testtable" where the sex is 'Male' , the following mongodb command can be used :

>db.testtable.find({"sex":"Male"}).count()

Output of the command:

> db.testtable.find({"sex":"Male"}).count()
3

MongoDB count() example with conditional operator

If we want to get the number of documents in the collection "testtable" where the sex is 'Male' and education is 'M.C.A.' , the following mongodb command can be used :

>db.testtable.find({"sex":"Male","education":"M.C.A."}).count()

Output of the command:

			
>db.testtable.find({"sex":"Male","education":"M.C.A."}).count()
1

MongoDB count() example with skip()

The count() by default ignores the skip() and limit() parameters.

If we want to get the number of documents after skipping the first document from the collection "testtable" where the sex is 'Male', the following mongodb command can be attempted :

>db.testtable.find({"sex":"Male"}).skip(1).count()

Output of the command:

> db.testtable.find({"sex":"Male"}).skip(1).count()
3

The above statement ignores the skip() parameter.

MongoDB count() example with true parameter

The 'true' parameter with count() can be used to consider the skip and limit values in the calculation.

If we want to get the number of documents after skipping the first document and consider it in the calculation, from the collection "testtable" where the sex is 'Male', the following mongodb command can be used:

>db.testtable.find({"sex":"Male"}).skip(1).count(true)

Output of the command:

			
> db.testtable.find({"sex":"Male"}).skip(1).count(true)
2

MongoDB count() example with dot notation

If we want to get the number of documents in the collection "testtable" where the 'community_name' under the 'extra' is 'MODERN MUSIC', the following mongodb command can be used :

>db.testtable.find({"extra.community_name" : "MODERN MUSIC"}).count()

Output of the command:

> db.testtable.find({"extra.community_name" : "MODERN MUSIC"}).count()
2

Previous: $allElementsTrue
Next: User Management Commands



Follow us on Facebook and Twitter for latest update.