Skip to content

All about MongoDB! Notes and examples

Notifications You must be signed in to change notification settings

Coryf65/AllAbout_MongoDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

All about MongoDB

  • 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...

About MongoDB

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

Installing locally

  • Install and Run as a Windows Service (suggested way, easier)

  • Run as a local user

$ mongod.exe --config mongod.cfg

How to connect

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 and Collections

  1. Documents ?

    • Field-Value Pairs
    • Stored in JSON-like BSON (Binanary JSON)
    • similar to a row in MSQL Server
  2. 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
  3. 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 });
  1. What can we store ?
  • lots of data with various types

  • text, numbers, arrays,

Querying

  • Query sent -> Cursor created -> Options and operators -> Results returned

  • you can chain calls, sort of like method chaining

  1. Sort
db.recipes.find({}, { "title": 1 }).sort( {"title": 1 });
  1. Limit
db.recipes.find({}, { "title": 1 }).limit(2);
  1. Skip
db.recipes.find({}, { "title": 1 }).skip(2);

Operators and Arrays

  1. $gt

    • $gt selects those documents where the value of the field is greater than (i.e. >) the specified value.
  2. $lt

    • $lt selects the documents where the value of the field is less than (i.e. <) the specified value.
  3. $lte

    • $lte selects the documents where the value of the field is less than or equal to (i.e. <=) the specified value.
  4. $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
  5. $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.

Example Get any recipe with egg as an ingredient.

db.recipes.find({ "ingredients.name": "egg"}, {"title": 1});

Updating Documents

Mongo also uses an Upsert, update or insert

  1. $set

    • The $set operator replaces the value of a field with the specified value.
{ $set: { <field1>: <value1>, ... } }
  1. $unset

    • The $unset operator deletes a particular field. Consider the following syntax:
{ $unset: { <field1>: "", ... } }
  1. $inc

    • The $inc operator increments a field by a specified value and has the following form.
{ $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }

Updating Arrays

  1. $push

    • The $push operator appends a specified value to an array.

The $push operator has the form:

{ $push: { <field1>: <value1>, ... } }
  1. $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>, ... } }

Deleting Documents

  1. 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.

  2. db.collection.deleteMany()

    • Delete all documents that match a specified filter.

    New in version 3.2.

  3. db.collection.remove()

    • Delete a single document or all documents that match a specified filter.

Data Modeling and Schema

"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 ?

Indexes / Indecies

Compound indexes, regular indexes, and geo-spatial indexes

  • See our indexes

    • db.recipes.getIndexes() or db.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");

Grid FS

  • 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())

Server Administration

Reference to the Mongo Docs here

  1. Configuration

    • you can configure anything in the CLI, or in the config file which varies location based on OS
  2. 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

  3. Sharding

    • more info here

    • Partitioning data across multiple many servers, and spread across usage needs

    • can be complicated use only if needed

  4. Authentication and Authorization

    Example of creating a user, MongoDocs for creating a user

    db.createUser(
        {
            user: "tester",
            pwd: paaswordPrompt(),
            roles: [ { role: "userAdmininAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
        }
    )
  5. Backups

    1. 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();

    2. Mongodump / mongorestore

About

All about MongoDB! Notes and examples

Topics

Resources

Stars

Watchers

Forks