-
Notes for MongoDB
-
Docs for MongoDB here
-
If you have used a Relational SQL DB concepts and terms, see the terminology comparison here
-
No Locks or Transactions
-
Works with many programming languages all listed here some examples
Python
,Node.js
,PHP
,C#
,Go
,Java
,Ruby
and many more...
MongoDB stores data in flexible, JSON-like documents, meaning fields can vary from document to document and data structure can be changed over time
The document model maps to the objects in your application code, making data easy to work with
Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze your data
MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution are built in and easy to use
MongoDB is free to use. Versions released prior to October 16, 2018 are published under the AGPL. All versions released after October 16, 2018, including patch fixes for prior versions, are published under the Server Side Public License (SSPL) v1.
from: MongoDB Official Website
here
-
Install and Run as a Windows Service (suggested way, easier)
-
Run as a local user
$ mongod.exe --config mongod.cfg
-
Using Compass, a tool with a GUI
-
CLI
-
VSCode Mongo Extension : VS Marketplace Link
example query, connected using VSCode
// MongoDB Playground
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.
// The current database to use.
use('LearningMongo');
// Search for documents in the current collection.
db.getCollection('examples')
.find(
{
/*
* Filter
* fieldA: value or expression
*/
},
{
/*
* Projection
* _id: 0, // exclude _id
* fieldA: 1 // include field
*/
}
)
.sort({
/*
* fieldA: 1 // ascending
* fieldB: -1 // descending
*/
});
-
Documents ?
- Field-Value Pairs
- Stored in JSON-like BSON (Binanary JSON)
- similar to a row in MSQL Server
-
Collectons ?
- Grouping related Documents
- can cap a collection to auto delete oldest data
- not all collections need to have the same data types, and data
- similar to tables
-
Searching for a Document
-
Find()
db.collection.find(query, projection)
Search for any thing the contains taco, similar to a like in TSQL
we are using Regex here
db.recipes.find({ "title": { $regex: /taco/i }}, { "title": 1 });
- What can we store ?
-
lots of data with various types
-
text, numbers, arrays,
-
Query sent -> Cursor created -> Options and operators -> Results returned
-
you can chain calls, sort of like method chaining
- Sort
db.recipes.find({}, { "title": 1 }).sort( {"title": 1 });
- Limit
db.recipes.find({}, { "title": 1 }).limit(2);
- Skip
db.recipes.find({}, { "title": 1 }).skip(2);
-
$gt
$gt
selects those documents where the value of the field is greater than(i.e. >)
the specified value.
-
$lt
$lt
selects the documents where the value of the field is less than(i.e. <)
the specified value.
-
$lte
$lte
selects the documents where the value of the field is less than or equal to(i.e. <=)
the specified value.
-
$all
- The
$all
operator selects the documents where the value of a field is an array that contains all the specified elements. To specify an$all
expression, use the following prototype
- The
-
$in
- The
$in
operator selects the documents where the value of a field equals any value in the specified array. To specify an$in
expression, use the following prototype. - Matches any of the values specified in an array.
- The
Example Get any recipe with
egg
as an ingredient.
db.recipes.find({ "ingredients.name": "egg"}, {"title": 1});
Mongo also uses an Upsert, update or insert
-
$set
- The $set operator replaces the value of a field with the specified value.
{ $set: { <field1>: <value1>, ... } }
-
$unset
- The $unset operator deletes a particular field. Consider the following syntax:
{ $unset: { <field1>: "", ... } }
-
$inc
- The $inc operator increments a field by a specified value and has the following form.
{ $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }
-
$push
- The $push operator appends a specified value to an array.
The $push operator has the form:
{ $push: { <field1>: <value1>, ... } }
-
$pull
- The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
The $pull operator has the form:
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
-
db.collection.deleteOne()
- Delete at most a single document that match a specified filter even though multiple documents may match the specified filter.
New in version 3.2.
-
db.collection.deleteMany()
- Delete all documents that match a specified filter.
New in version 3.2.
-
db.collection.remove()
- Delete a single document or all documents that match a specified filter.
"Data that is accessed together should be stored together." - MongoDB
-
One to One
- We would want to generally store the ingredients in the recipes, or store the address in the users
-
One to Many
- one recipe to many comments
- example have a recipe with the top 3 comments and have a load more button that will load an all comments from another documents
-
we do have some gotchas,
- Will your data change ?
- Can you store some of your data ?
Compound indexes, regular indexes, and geo-spatial indexes
-
See our indexes
db.recipes.getIndexes()
ordb.recipes.getIndicies()
-
create an index
- create an index based in cook_time asc
db.recipes.createIndex({"cook_time" : 1})
-
remove and index, need the name of a index
db.recipes.dropIndex("cook_time_1");
-
use to store entire files
-
breaks files into chunks and streams them back to you
Examples:
Getting a file saved in mongo
mongofiles get seattle.jpg --db=files
Save a file into our DB
mongofiles put taco.jpg --db=files
List out all file from a DB
mongofiles list --db=files
Example using Python and MongoFiles
#! /usr/bin/python import pymongo import gridfs client = pymongo.MongoClient("localhost", 27017) db = client.files fs = gridfs.GridFS(db) file_id = fs.put(b"Test Test Test 123...", filename="testing.txt") print(fs.list()) print(fs.get(file_id).read())
Reference to the Mongo Docs here
-
Configuration
- you can configure anything in the CLI, or in the config file which varies location based on OS
-
Replication
-
more info here
-
you can have primary and secondary nodes, useful for scaling
-
place them inside seperate Docker containers and have those on many servers
-
-
Sharding
-
more info here
-
Partitioning data across multiple many servers, and spread across usage needs
-
can be complicated use only if needed
-
-
Authentication and Authorization
-
Mongo DB Docs Authentication & Authorization
-
Authentication = Logging in
-
Authorization = Limiting Access
Example of creating a user, MongoDocs for creating a user
db.createUser( { user: "tester", pwd: paaswordPrompt(), roles: [ { role: "userAdmininAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } )
-
-
Backups
- Mongo Docs here
-
Easiest way
- enter db, stop writes while doing so prevents writing to disk saves to ram
db.fsyncLock();
. then Copy all the files and put into a backup location.
example to copy files
cp -R /data/db/* .../someplace
Then enable writes again
db.fsyncUnlock();
- enter db, stop writes while doing so prevents writing to disk saves to ram
-
Mongodump / mongorestore