What is MongoDB???
It’s a “schema-less NoSQL document database”. You can store JSON documents, with the structure of the documents can be different. Structure is not enforced such as with an SQL database.
Databases:
MongoDB databases hold one or more collections, which in turn store documents.
Collections:
These are used to store documents, similar to a table in a relational database. If you have a database, you can automatically create a collection when you tell Mongo to store something in it (Such as from Node via Mongoose).
Documents:
The documents inside collections are stored as BSON documents. BSON is short hand for Binary JSON, you can read the specs here. BSON can represent JSON documents and other data types in binary form. Most common to see people sending JSON documents to MongoDB, though it can hold other types of documents. The size limit for a document is 16MB. You can store larger sizes using the GridFS API. It must have the _id as the first field, it is used as the primary key.
Documents have a simple structure of field: value
, much like the JavaScript object format of key: value
. These documents can contain other documents as the value of the field, along with arrays, and arrays of documents. Here is an example from the MongoDB website of a document:
var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"),
name: { first: "Alan", last: "Turing" },
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}
The field names must be strings, and _id is reserved for the primary key and must have a unique value. MongoDB creates this automatically when storing a new document if you do not provide one.
Access document elements:
Dot Notation! If you have a document with called newDoc79 and a field called “name” you could use newDoc79.name to get the value of the name field. For arrays, it would be newDoc.arrayName.2, easy enough!
Mongoose!
Mongoose is an Object Document Modeling (ODM) for use with Node and its MongoDB driver. Because MongoDB is schema-less, documents can have different sets of fields which have different types of data as their value. So you could save anything to a document, with the next document in that collection completely different. However, this can get messy. For example, if you have a Users collection, you probably want each User document inside of it to be in the same format. Mongoose to the rescue! It helps with Schemas, Validations, Methods, and Returning Queries.
Schemas:
Mongoose allows you to define Schemas for MongoDB, fields and data types for the values. Usually stored in a folder called “models” in Node projects.
Validation:
This is super handy for receiving POST data such as usernames and passworsd, Mongoose has it’s own that you can use.
Methods:
Mongoose has pre and post save operations you can use. So before saving data, you could validate it. Perhaps check a password. After saving, it can return a success message.
Queries:
Mongoose returns the updated object itself for an update query, while MongoDB would return an object that only had the number of objects modified and success or not.
Mongoose Schema Example:
From a Node project:
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
max: 255,
min: 6
},
email: {
type: String,
required: true,
max: 255,
min: 6
},
password: {
type: String,
required: true,
max: 1024,
min: 6,
},
date: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model('User', userSchema);mon