About MongoDB
MongoDB is an open source database management system (DBMS) that uses a document-oriented data model. It is considered a NoSQL database, as it doesn't use the relational model, and therefore doesn't use SQL as its query language.
The document-oriented model enables MongoDB to store semi-structured data which doesn't require a fixed schema. It can achieve this through the use of JSON documents.
MongoDB is used by some of the largest companies in the world, including Facebook, Google, Nokia, MTV Networks, Cisco, Forbes, and many more.
MongoDB is also a cross platform DBMS, currently supporting Windows, Mac, Solaris, and various Linux distributions at the time of writing.
A MongoDB database is different to a relational database in that, MongoDB uses a document-oriented model to store data. In the document-oriented model, data is stored within documents of a collection. In the relational model, data is stored within rows of a table.
The Collections
In MongoDB, a collection is a group of documents. A collection typically contains documents that have a similar topic (like Users, Products, Posts, etc).
Collections are therefore, in many ways, similar to tables from the relational model.
The Documents
In MongoDB, documents are stored as JSON documents. JSON (JavaScript Object Notation) is a standard that facilitates data interchange. JSON documents are similar to XML documents in that data can be presented in a hierarchical way, and can be read by humans and computers alike.
Here's an example of a JSON document. This is what documents inside a MongoDB database look like.
The _id
field is the unique identifier for a document. MongoDB allows for each document to be retrieved/referenced using this field. You can supply this or let MongoDB generate it.
By using JSON, query results can be easily parsed, with little or no transformation, directly by JavaScript and most popular programming languages. This is because JSON documents use the name/pair, and array conventions that are familiar to most popular programming languages such as C, C++, C#, Java, JavaScript, Perl, Python, and many others. This reduces the amount of business logic that needs to be built into applications that use MongoDB.
Behind the scenes, MongoDB actually stores the JSON documents in a binary-encoded format called BSON. BSON extends JSON through supporting additional data types and to be efficient for encoding and decoding within different languages.
Schemaless
Each JSON document in a collection can contain its own structure. Therefore, there is no fixed schema that limits the type of data that can be entered into a MongoDB database.
This is in contrast to a relational database where, you must create the schema first (i.e. define the tables, columns, data types, etc), before entering any data. If data doesn't adhere to the schema, it doesn't go into the database.
In a MongoDB database, there is no rule to say which fields, or how many fields, each document should have. For example, one document from a collection might contain name, address, and phone number, while another document might contain name and email address.