Seven NoSQL Databases in a Week
上QQ阅读APP看书,第一时间看更新

MongoDB documents

Data in MongoDB is actually stored in the form of documents. The document is a collection of key-value pairs. The key is also known as an attribute.

Documents have a dynamic schema and documents in the same collection may vary in field set.

Here is the structure of a MongoDB document:

MongoDB documents have a special field called _id._id is a 12-byte hexadecimal number that ensures the uniqueness of the document. It is generated by MongoDB if not provided by the developer.

Of these 12 bytes, the first 4 bytes represent the current time stamp, the next 3 bytes represent the machine ID, the next 2 bytes represent the process ID on the MongoDB server, and the remaining 3 bytes are simple auto-increment values, as shown in the following diagram:

_id also represents the primary key for MongoDB documents.

Let's look at a quick comparison between SQL and MongoDB:

SQL

MongoDB

Database

Database

Table

Collection

Row

Document

Column

Field

 

Here are some of MongoDB's advantages over RDBMS:

  • Collections in the MongoDB database are schemaless. Documents inserted in collections can have different sets of fields.
  • The structure of a single-document object is simple and clear.
  • Complex joins are not required as the MongoDB database supports an embedded document structure.
  • MongoDB has rich query support. MongoDB supports dynamic queries on a database.
  • MongoDB is easy to scale.
  • Conversion or mapping between database objects and application objects is simple as most of the application supports JSON mapping with database objects.
  • Integrated memory support allows the user to access data in a much faster way.

The following are the uses of MongoDB:

  • Document-oriented storage
  • Index on any attribute
  • Replication and high availability
  • Auto-sharding
  • Rich query support
  • Professional support from MongoDB

The applications of MongoDB are:

  • User profiles: Authentication tools and LDAP are good for authentication and authorization, but data, such as rewards, criminal records, promotions, phone numbers, and addresses are added day by day. Other databases are not able to adopt such quick-changing data. We can use MongoDB dynamic documents to store such data over time in the document.
  • Product and catalog data: In e-commerce companies or chemical companies, many new products are getting added every day. Each time a new product is added, it is not easy to change schema quickly. In these scenarios, using a document-based database is easier than using any other traditional database.
  • Metadata: We often require metadata that describes our data. In such scenarios, a graph-based database is a good choice, but we can also use MongoDB for these applications.
  • Content: MongoDB is mainly a document database. It is great for serving text as well as HTML documents. Also, it provides fine control over storing and indexing contents.

The limitations of MongoDB are:

  • The maximum document size supported by MongoDB is 16 MB.
  • The maximum document-nesting level supported by MongoDB is 100.
  • The maximum namespace (database + collection name) supported by MongoDB is 123 characters.
  • The database name is limited to 64 characters.
  • If we apply an index on any field, that field value cannot contain more than 1024 bytes.
  • A maximum of 64 indexes are allowed per collection and a maximum of 34 fields are allowed in compound indexes.
  • A hashed index cannot be unique.
  • A maximum of 12 nodes are allowed in a replica set.
  • A maximum of 512 bytes are allowed for shard keys.
  • You cannot rollback automatically if data is more than 300 MB. Manual intervention is needed in such cases.

Now we will go through the MongoDB CRUD operations.