Open In App

How Many Documents are in a MongoDB Collection?

In a general context, a document is a written, drawn, or recorded representation of data. In MongoDB, a document is a unit of data storage and follows a JSON-like format with key-value pairs, providing flexibility in storing various data types.

The below article provides an overall overview of how MongoDB documents work, its size limitations, and monitoring strategies. We’ll explore document size limitations and will understand the number of documents inside a collection and its related aspects.



Collections and Documents in MongoDB

relationship between collections and documents in MongoDB:

The above diagram shows the relationship between the collections and documents in MongoDB.

Each big rectangle shows a collection namely : Collection A and Collection B and each of these collections can have various documents as shown.



The documents within a collection can have different structures; hence, the variation in the number and types of fields in each document.

Below represents how a document can contain various of structures.

{
"StudentID": ObjectId("5f5e953b7f08de2c4a08b5a6"),
"name": "Milind_Gupta",
"age": 24,
"address": {
"Delhi",
},
"comments": [
{
"text": "Great work!",
"author": "Alice"
},
{
"text": "Keep it up!",
"author": "Bob"
}
],
"registrationDate": ISODate("2022-01-15T08:00:00Z")



Explanation: The above is an example showing how a document would be in a MongoDB, which illustrates how MongoDB can accomodate various types types of data types like strings, numeric data , objects , arrays, dates, boolean , embedded documents.

If we see theoretically then the number of documents to store in a MongoDB will be practically unlimited , that is depending upon the disk space and the system resources. MongoDB is designed to hold large number amounts of data.

MongoDB can handle terabytes or even petabytes of large data which is basically depending upon the configuration and system handling ability.

Example: Collections and Documents

Consider two MongoDB databases, MongoDB (A) and MongoDB (B), each containing a collection with multiple documents (Document 1, Document 2, Document 3). This hierarchical structure follows Database > Collection > Document.

Here the actual documents can vary and can be very large depending on various types of factors such as disk space, system resources and other considerations. MongoDB is designed to handle such scalability and such large data sets.

How to Find the Number of Documents using Nodejs

Let’s understand step by step, how to find the number of documents in it.

Step1: First we need to install MongoDB package through.

npm install mongodb

Step 2: Note down the mongoDB connection URL and Collection name either from MongoDB Atlas or the Web Version.

const uri = ‘mongodb://localhost:27017/your_database_name’; // Replace with your MongoDB connection URI
const collectionName = ‘your_collection_name’; // Replace with your collection name

Step 3: You need to create an object of the MongoDB inorder to check the documents availability there.

// Create a new instance of the MongoDB client
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

Step 4: Connect to the MongoDB Server,

Use await client.connect() to establish a connection to the MongoDB server specified in the URI.

Step 5: For Count Documents,

Use the countDocuments method on the collection to find the total number of documents. The empty {} parameter in countDocuments({}) means that we’re counting all documents without any specific filter.

const documentCount = await collection.countDocuments({});
The above steps clearly shows how we can connection our node app to MongoDB database, then accessing it’s collection and the counting the number of documents inside it.

MongoDB Document Properties and Limitation

Document Size Limitations

MongoDB contains various types of different data types having different sizes and limitations. It’s our responsibilty to understand how to deal with these limitations to provide an effective storage strategy.

Monitoring and Maintenance

Monitoring and Maintenance is needed for securely deploying our MongoDB documents.

Monitoring

Maintenance

Conclusion

In conclusion, understanding the concepts of collections is necessary to know how documents will be stored in MongoDB, and understanding how mongodb having the flexibility in the document structure within the collection. The discussion covers the ordered relationship between documents and collections, and the provided Node.js example describe a straightforward method for counting documents.

The relationship between documents and collections are ranked in ordered and with collections holding lot of documents. The Nodejs version shows how one can find the exact number of documents in their mongodb using collection.countDocuments({}).


Article Tags :