• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Collections in MongoDB

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 namespaceDescription
dbname.system.namespaceslist of all namespaces
dbname.system.indexeslist of all indexes
dbname.system.profilestores database profiling information
dbname.system.userslist of users who may access the database
dbname.local.sourcesstores replica slave configuration data and state
dbname.local.sourcesstores 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

Summary
Review Date
Reviewed Item
Collections in MongoDB
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Collections in MongoDB"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.