Menu

MongoDB

What is MongoDB?

Share on

What is MongoDB?

MongoDB is an open-source document-oriented NoSQL database system. Data is stored using JavaScript Object Notation(JSON)-like structures that can be specified at the time of data storage.

Each document can have its own structure with as much or as little complexity as needed. MongoDB provides non-SQL methods and commands to manage and query data programmatically or interactively. MongoDB is known for its fast performance, scalability, and for enabling a rapid development pace.

RELATED ON PRISMA.IO

If you're using MongoDB, checkout Prisma's MongoDB connector! You can use the Prisma Client to manage production MongoDB databases with confidence.

To get started working with MongoDB and Prisma, checkout our getting started from scratch guide or how to add to an existing project.

Origin story

MongoDB was founded in 2007 by Dwight Merriman, Eliot Horowitz, and Kevin Ryan. They previously worked at an internet advertising company that was serving 400,000 ads per second.

The team was developing many custom data stores to accommodate this traffic. They regularly ran into struggles around scalability and agility, which led to the inspiration to create a database that solved for these issues. And voila, MongoDB.

How it works

To achieve better scalability and agility, MongoDB works with the JSON variant, Binary JSON, or BSON. BSON accommodates more data types than JSON, giving more flexibility to the types of data that can be stored in MongoDB.

MongoDB data structure

Rather than tables made up of rows and columns, MongoDB has collections made up of documents. Documents are comprised of field and value pairs. In comparison to relational databases, the pieces that make up a MongoDB database can loosely be thought of as follows:

RelationalDocument
Table=Collection
Row=Document
Column=Field

The basic structure looks as follows:

{
field1: value1,
field2: value2,
field3: value3,
...
fieldN: valueN
}

Documents look very similar to a JSON object. Documents can hold any of the available BSON data types as well as other documents, arrays, and arrays of documents. This JSON-style format directly maps to native objects in most modern programming languages, making it a natural choice for developers.

An example would look like:

var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"),
name: { first: "James", last: "Joyce" },
birth: new Date('Feb 02, 1882'),
death: new Date('Jan 14, 1941'),
books: [ "Ulysses", "Dubliners","Finnegan's Wake" ],
sales : NumberLong(1250000)
}
  • _id holds an [ObjectId].
  • name holds an embedded document that contains the fields first and last.
  • birth and death hold values of the [Date] type.
  • books holds an array of [strings].
  • sales holds a value of the [NumberLong] type.

Depending on the data model, rather than a single document housing all of the information on James Joyce and his work, a relational database could potentially have an author table and a book table that is joined with something like author_id.

In this particular simple use case, the document model simplifies data access and keeps data that is accessed together, stored together.

MongoDB document vs SQL table record

Expanding on the previous example, let's look at how a document in MongoDB looks compared to the same record in a relational database. In a relational database, a table storing records might look something like this:

primary_key | first_name | last_name | age | email | phone_number |
----------- | ---------- | --------- | --- | -------------------- | ------------ |
1 | Tom | Brown | 38 | tom.b@email.com | 555-3802 |
2 | Sarah | Green | 84 | (NULL) | 555-8088 |
3 | Sam | White | 22 | sammi@123.org | 555-1234 |

Whereas, as we saw with our author document previously, a collection of similar documents in MongoDB might be modeled in JSON like this:

{
primary_key: 1,
first_name: "Tom",
last_name: "Brown",
age: 38,
email_address: "tom.b@email.com",
phone_number: "555-3802"
},
{
primary_key: 2,
first_name: "Sarah",
last_name: "Green",
age: 84,
phone_number: "555-8088"
},
{
primary_key: 3,
first_name: "Sam",
last_name: "White",
age: 22,
email_address: "sammi@123.org",
phone_number: "555-1234",
notes: [
"High value customer",
"Has a dog named Fido"
]
}

Deciding on a document database versus a relational database comes down to the type of data that the database is storing and how it should be accessed. MongoDB's documents promote flexibility over rigidity and ease the way data is accessed from an application.

MongoDB query language

Since MongoDB databases are document-oriented, the data does not typically follow a pre-defined schema like a relational database. This makes accessing data different then when querying a relational database with structured query language (SQL). MongoDB has its own query language simply called MongoDB Query Language (MQL).

Similar to SQL, MQL also allows a user to access data under certain specifications. You can control for returning documents that match criteria relevant to the need. We won’t go into the finer details of querying in MongoDB in this guide, but you can read more about it in our querying in MongoDB guide.

The basic syntax of a query in MongoDB will look something like this:

db.<collection_name>.find()

To produce a nicer format for query results, you can use .pretty().

db.<collection_name>.find().pretty()

Conclusion

MongoDB has become the most popular NoSQL database on the market. Developers enjoy working with MongoDB because of its ability to keep them working flexibly and with agility.

MongoDB provides an alternative to the rigidity of relational databases and is better suited for applications with evolving schemas. Checkout out some of our other MongoDB content for in depth guides on getting started with MongoDB, indexes, transactions, and more.

RELATED ON PRISMA.IO

If you're using MongoDB, checkout Prisma's MongoDB connector! You can use the Prisma Client to manage production MongoDB databases with confidence.

To get started working with MongoDB and Prisma, checkout our getting started from scratch guide or how to add to an existing project.

About the Author(s)
Alex Emerich

Alex Emerich

Alex is your typical bird watching, hip-hop loving bookworm that also enjoys writing about databases. He currently lives in Berlin, where he can be seen walking through the city aimlessly like Leopold Bloom.