Skip to content

1.0 Essentials

Olga Arsenieva edited this page Nov 19, 2018 · 1 revision

1.0 MongoDB Essentials

Terminology Translation

RDBMS MongoDB
Database Database
Table Collection
Row Document
Index Index
JOIN Embedded document, document references or $lookup to combine data from different collections
Mongo DB supports a hundred levels of depth for your documents​

See More: Terminology and Concepts

See More: SQL to MongoDB Mapping Chart

  • String− This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid.
  • Integer− This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.
  • Boolean− This type is used to store a boolean (true/ false) value.
  • Double− This type is used to store floating point values.
  • Min/ Max keys − This type is used to compare a value against the lowest and highest BSON elements.
  • Arrays− This type is used to store arrays or list or multiple values into one key.
  • Timestamp− ctimestamp. This can be handy for recording when a document has been modified or added.
  • Object− This datatype is used for embedded documents.
  • Null− This type is used to store a Null value.
  • Symbol− This datatype is used identically to a string; however, it's generally reserved for languages that use a specific symbol type.
  • Date − This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it.
  • Object ID − This datatype is used to store the document’s ID.
  • Binary data − This datatype is used to store binary data.
  • Code − This datatype is used to store JavaScript code into the document.
  • Regular expression − This datatype is used to store regular expression.

Query Language

Query Language

See More: Query Language

See More: SQL to MongoDB Mapping Chart

Why MongoDB is effective

RDBMS Data

  • Separate tables for different data
  • Tied together by primary key
  • Complex representation

SQL Query for data from RDBMS

  • Data is discrete in several tables​
  • Complex to select​

RDB_Table

RDB_Table

Select data from MongoDB

  • Data is in only an object​
  • Easy to handle​

NoSQL_Document

See More: Query Language

Aggregation

  • Similar to GROUP BY function in SQL​

Aggregation

See More: SQL to Aggregation Mapping Chart

Performance

Index

  • 64 indexes
  • Single field
  • Compound
  • Unique
Mongo does support up to 64 indexes per collection​

Sharding

  • Partition data onto different machines
  • Provides scalability via software
  • Autosharding supported by Mongo
  • Challenging to set up

Using sharding means that you can scale your application across several smaller systems rather than investing in a larger, more expensive solution.​

While sharding is aimed at scalability, it's arguably even more important to ensure the uptime of your system, and for this, Mongo provides replication.​

Replication

  • Reliability
  • Maximizes uptime
  • Replica set
  • Automation failover

With MongoDB, you set up a replica set for your database with a primary server and some number of secondary servers, which keep copies of the primary servers data. If your primary server fails, one of the secondary servers will be promoted to the primary slot, and the database will continue to act as usual. When the original primary comes back online, it will be added back to the system as a new secondary server in the system.

Debugging​​

db.number.explain("executionStates")​

Debug

Stored Procedures

  • Create:

function addNumbers( x , y ) {
    return x + y;
}
  • Save:

> db.system.js.save({_id:"addNumbers", value:function(x, y){ return x + y; }});
  • Utilize:

> db.system.js.find()
{ "_id" : "addNumbers", "value" : function cf__3__f_(x, y) {
    return x + y;
} }
  • Result:

> db.eval('addNumbers(17, 25)');
42

See More: Getting Started With Stored Procedures in MongoDB

Clone this wiki locally