Mongo/Mongoose

Mongo

MongoDB is a NoSQL cross-platform document-oriented database.

Why USE IT

NoSQL databases offer many benefits over relational databases. NoSQL databases have flexible data models, scale horizontally, have incredibly fast queries, and are easy for developers to work with.

 

Flexible data models

NoSQL databases typically have very flexible schemas. A flexible schema allows you to easily make changes to your database as requirements change. You can iterate quickly and continuously integrate new application features to provide value to your users faster.

Horizontal scaling

Most SQL databases require you to scale-up vertically (migrate to a larger, more expensive server) when you exceed the capacity requirements of your current server. NoSQL databases allow you to scale-out horizontally, meaning you can add cheaper, commodity servers whenever you need to.

Fast queries

Queries in NoSQL databases can be faster than SQL databases. Why? Data in SQL databases is typically normalized, so queries for a single object or entity require you to join data from multiple tables. As your tables grow in size, the joins can become expensive. However, data in NoSQL databases is typically stored in a way that is optimized for queries. The rule of thumb when you use MongoDB is Data is that is accessed together should be stored together. Queries typically do not require joins, so the queries are very fast.

Easy for developers

Some NoSQL databases like MongoDB map their data structures to those of popular programming languages. This mapping allows developers to store their data in the same way that they use it in their application code. While it may seem like a trivial advantage, this mapping can allow developers to write less code, leading to faster development time and fewer bugs.

Short Demo

  • show dbs
  • use <name of db>
  • db.createCollection(<name of collection>)
  • show collections
  • db.collection.insert()
  • db.collection.insertMany()
  • db.collection.find()
  • db.collection.findOne()
  • db.collection.remove()
  • db.dropDatabase()

Let's talk about Mongoose

What is Mongoose?

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.

Terms to Get Used To:

  • Database — Can contain one or more collections.
  • Collection — Can contain one or more documents.
  • Document — Key/Value pair list or array of nested documents.
  • Schema — Specific data structure of a document.
  • Model — Constructor that takes a specific schema and create an instance of a document.

 

Models are responsible for creating and reading documents.

Mongo/Mongoose

By JD Richards