Collections in MongoDB

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

Collection

A collection may store number of documents. A collection is analogous to a table of a RDBMS. A collection may store documents those who are not same in structure. This is possible because MongoDB is a Schema-free database. In a relational database like MySQL, a schema defines the organization / structure of data in database. MongoDB does not require such a set of formula defining structure of data. So, it is quite possible to store documents of varying structures in a collection. Practically, you don't need to define a column and it's datatype unlike in RDBMS, while working with MongoDB. In the following code, it is shown that two MongoDB documents, belongs to same collection, storing data of different structures. A collection is created, when the first document is inserted.

Valid collection names

Collection names must begin with letters or an underscore. A Collection name may contain numbers. You can't use "$" character within the name of a collection. "$" is reserved. A Collection name must not exceed 128 characters. It will be nice if you keep it within 80/90 characters. Using a "." (dot) notation, collections can be organized in named groups. For example, tutorials.php and tutorials.javascript both belong to tutorials. This mechanism is called as collection namespace which is for user primarily. Databases don't have much to do with it. Following is how to use it programmatically :

db.tutorials.php.findOne()

These core tutorials will help you to learn the fundamentals of Mongo DB.
For an in-depth understanding and practical experience, explore Online Mongo DB Training.
 

capped collections

Imagine that you want to log the activities happening with application. you want to store data in the same order it is inserted. MongoDB offers Capped collections for doing so. Capped collections are collections which can store data in the same order it is inserted. It is very fixed size, high-performance and "auto-FIFO age-Out". That is, when the allotted space is fully utilized, newly added objects (documents) will replace the older ones in the same order it is inserted. Since data is stored in the natural order, that is the order it is inserted, while retrieving data, no ordering is required, unless you want to reverse the order. New objects can be inserted into a capped collection. Existing objects can be updated. But you can't remove an individual object from the capped collection. Using drop command, you have to remove all the documents. After drop, you have to recreate the capped collection. Presently, maximum size for a capped collection is 1e9(i.e. 1X109) for 32 bit machines. For 64 bit machines, there is no theoretical limit. Practically, it can be extended till your system resources permit. Capped collections can be used for logging, caching and auto archiving.

Use number of collections instead of one

This omits the requirement if creating index since you are not storing some repeating data on each object. If applied on a suitable situation, it can enhance the performance.

Metadata

Information about a database is stored in certain collections. They are grouped in system namespace, as

dbname.system.*

The following table shows the collections and what they store

Collections with namespace Description
dbname.system.namespaces list of all namespaces
dbname.system.indexes list of all indexes
dbname.system.profile stores database profiling information
dbname.system.users list of users who may access the database
dbname.local.sources stores replica slave configuration data and state
dbname.local.sources stores replica slave configuration data and state

There are two more options to store metadata : database.ns files stores additional namespace / index metadata if exists. Information on the structure of a stored object is stored within the object itself.

For indepth understanding click on

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/1

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox