Welcome to a tutorial on the Overview of MongoDB.
MongoDB consists of a set of databases, in which each database consists of other Collections. However, in MongoDB data is stored in collections. Check out the figure below of the typical database structure in MongoDB.
Database in MongoDB is simply a container for data collection. In subsequent tutorials, you will learn how to create a new Database, drop a Database, and also, how you can make use of an existing Database.
The collection is a set of MongoDB documents, which are equivalent to the row of data in tables in RDBMS. However, collections in MongoDB do not relate to any set schema as compared to RDBMS. Rather, they are ways of storing related data.
What it means to be schemaless, is that any type of Document can be saved in a collection, however, the similarity is recommended for index efficiency. Also, a document can have a maximum size of 4MB.
The namespace can be used to logically group and nest collections. E.g. we can have one collection named (INSERT NAME) to save user information, then there can be others such as the (ADD NAME OF DATABBASE.db) and (ADD NAME OF DATABBASE.db) to store forum questions and answers respectively.
Also, when we create an Index on a namespaced collection, it will only apply to that namespace.
Note that a collection is physically created as soon as the first document is created in it.
However, one might be wondering why to create multiple collections with a different namespace, when we can keep any form or data in a single collection itself. It is because MongoDB does not index attributes for totally unrelated documents. Hence, it's best practice to keep related data in collections.
Document in MongoDB is simply a set of key-value pairs, and they will have dynamic schema meaning that the documents in the same collection do not need to possess the same set of fields.
Although MongoDB is considered a schema-less database, that says that each collection can hold different types of objects. In MongoDB, every object in a collection is known as a Document, which is represented in a JSON-like (JavaScript Object Notation) structure( i.e. a list of key-value pairs).
Data is stored and queried in BSON, which is its binary representation of JSON-like data.
From the above figure, not that the field _id represents the primary key identifier of the given document. Also, MongoDB stores the values in the form of arrays of values. But, any type of data can be stored as values, and nothing is needed to be pre-defined.
In other words, the type of data doesn't need to be predefined to be stored, as you can store anything you want.
Documents are not identified by a simple ID, but by what is known as an object identifier type. The default Id is a combination of machine identifier, timestamp, and process id to keep it unique, but users are allowed to make changes.
{ _id : 112233,
name : "Alex",
education : [
{
year : 2022,
course : "Master in Computer",
college : "ICT"
},
{
year : 2022,
course : "MS",
college : "Harvard College"
}
]
}
Remember that the value of fields in a document can be anything, including other documents, arrays, and arrays of documents, date object, a String, and so on.
var mydoc = {
_id : ObjectId("5099803df3f4948bd2f98391"),
name : { first: "Ada", last: "Turing" },
birth : new Date('Jun 23, 1912'),
death : new Date('Jun 07, 1954'),
contribs : [ "Turing machine", "Turing test", "Turingery" ],
view : NumberLong(1250000)
}