MongoDB Concepts

Basics of MongoDB:

Database:
Is a physical container for collections.
Each database gets its own set of files on the file system.
A single MongoDB server typically has multiple databases.

Collection:
Is a group of MongoDB documents.
It is the equivalent of an RDBMS table.
A collection exists within a single database.
Collections do not enforce a schema.
Documents within a collection can have different fields.
Typically, all documents in a collection are of similar or related purpose.

Document:
Is effectively a JSON document with some additional features.
Is a set of key-value pairs.
Documents have dynamic schema.
Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.

MongoDB stores the documents in BSON (Binary JSON) format
– Supports length prefix on each value (Easy to skip over a field)
– Information about the type of a field value
– Additional primitives types not suppported by raw JSON like UTC date time, raw binary, and ObjectId


The data structure that defines a MongoDB document is called BSON (Binary JSON).
BSON is binary representation of JSON and also supports data types such as Date, which is not supported in plain JSON format.

MongoDB internally converts JSON to BSON and vice versa for performance benefits, although the user can save, query and retrieve as JSON.

MongoDB ObjectId
• Every document in Mongo must have an “_id” field that is unique
• Default ObjectId created by Mongo when you insert a document
• Example:
{
“_id” : ObjectId(“56ce74c0b02806eff4558f1f”),
“name” : “Uthapizza”,
“description” : “Test”
}

ObjectId is a 12 byte field:
• id.getTimestamp() returns the timestamp in ISO Date format.

var id = new ObjectId();
id.getTimestamp();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s