From the course: MongoDB C# Developer Associate Cert Prep
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Counting documents in a MongoDB collection - MongoDB Tutorial
From the course: MongoDB C# Developer Associate Cert Prep
Counting documents in a MongoDB collection
- Welcome back team. In this video, we'll learn how to count the number of documents that match a query. We'll count the documents in MongoDB using the countDocuments collection method. The syntax for countDocuments is db.collection.countDocuments. This method takes two parameters, a query document which allows us to select the documents that we'd like to count, and an options document which allows us to specify the counting behavior. Since the options document is very rarely used, we won't discuss it here. Let's see what count document does in action. We'll start with a basic example counting all the documents in a collection. Later, we'll count documents with a more complex query. In this example, we'll be working with a training database again but we'll be working with the collections called trips. Let's take a look at one of the documents. These documents include quite a few fields with information about the…