MongoDB count() Method Use db.collection.count() method to count the number of documents available in a collection. Syntax: db.COLLECTION_NAME.count(query) Example – Count Total Documents Use the following command from mongo shell to count the number of available documents in users collection. > db.users.count(); Output: 8 Example – Count with Find You can also use count() function with other functions like find. > db.users.find({“user_name”:”rahul”}).count() Output: 1
Read MoreTag: method
MongoDB – skip() method
MongoDB skip() Method Use skip() method to skip a number of documents. It works MongoDB find() method. Syntax: db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) Example For example you have 10 documents you collection users. Now get the 2 documents after 5 documents. Like you need to get 6’th and 7’th documents from the collection. db.users.find().limit(2).skip(5) The above query will skin first 5 documents from the collection and then show 2 documents. Output: { “_id” : ObjectId(“59a01c912427abe3470644de”), “id” : 1006, “user_name” : “peter”, “name” : [ { “first_name” : “Peter” }, { “middle_name” : “S” },…
Read MoreMongoDB – limit() method
MongoDB limit() Method Use limit() method to show the limited number of documents in a collection with find() method. Syntax: db.COLLECTION_NAME.find().limit(NUMBER) Example: For example users collection have 19 documents. Now you need to get only first 2 documents from collection. > db.users.find().limit(2); Output: { “_id” : ObjectId(“59a01b102427abe3470644db”), “id” : 1001, “user_name” : “rahul”, “name” : [ { “first_name” : “Rahul” }, { “middle_name” : “” }, { “last_name” : “Kumar” } ], “email” : “[email protected]”, “designation” : “Founder and CEO”, “location” : “India” } { “_id” : ObjectId(“59a01b2f2427abe3470644dc”), “id” :…
Read More