• Mongosh client

    • To print out commands in a json file to the mongoclient

      • cat FILE.json | mongosh

  • Administration

    • Launch mongo as: mongod –config mongod.conf OR mongod -f mongo.conf

    • To shutdown mongodb internally, issue the db.shutdownServer() operation against the admin database.

  • Roles and accounts

    • https://www.mongodb.com/docs/manual/reference/built-in-roles/

    • https://www.mongodb.com/docs/v5.0/tutorial/manage-users-and-roles/

  • Collections

    • Capped : To a certain size and last in first out

  • Query

    • https://docs.mongodb.com/manual/reference/operator/query/

      • https://www.mongodb.com/docs/manual/tutorial/query-embedded-documents/

      • https://www.mongodb.com/docs/manual/tutorial/query-arrays/

      • https://www.mongodb.com/docs/manual/tutorial/query-array-of-documents/

      • https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/

      • https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields/

      • https://www.mongodb.com/docs/manual/reference/method/db.collection.find/#std-label-find-projection

    • Query

      • A collection is named

      • A command is given where it can have sub commands

      • You can also have multiple commands where the output is fed to the next command

        • Such as : .db.DOC.find(name : 'mark').project( { name : 1)

        • project can have an aggregate command

      • Querying is one of formats

        • Against a value : Ex; db.people.find({ name : 'mark')}

          • against a value

        • Against against subdocuments. Ex: a docment called person, containing a subdocument 'contact', with field called 'name' and value 'mark'

          • db.person.find({ contact.name : 'mark')}

        • Against an array in the documents. “names” is an array.

          • Ex: db.person.find({ contact.names :[ 'mark', 'marcus'])}

        • Against an array of documents

          • Ex: Person is the document. Address is an array. In the array is a document with one of the fields named :”street”,

          • ex: db.person.find( { address : {street : 'main street'}})

      • For updates:

        • https://www.mongodb.com/docs/manual/reference/operator/update/positional/

        • https://www.mongodb.com/docs/manual/reference/operator/update/positional-all/#mongodb-update-up.---

        • The $ operator represents the first entry in a array if there are many entries in the array. It only updates the first match.

          • Ex: db.students.updateOne( { _id: 4, "grades.grade": 85 },{ $set: { "grades.$.std" : 6 } })

        • To update every match of documents for all elemts in the array

          • ex: db.students ( {grade :1}, { $inc : {test6.$[] : 10}

          • This adds 10 points to all students in 1st grade for test 6.

    • Multiple Insert with BulkWrite

      • db.people.bulkWrite([
        { insertOne : {person : “Mark”}}, {insertOne : {person : “Mark”}}
        ]

    • Update fields inc, many

      • db.people.update({person : “MARK”}, {$inc : {money : 100 }, {multi : 1}}

      • db.people.updateMany({person : “MARK”}, {$inc : {money : 100 }}

    • hint index

      • Use hint to override the index choice
        https://docs.mongodb.com/manual/reference/method/cursor.hint/

    • Documents missing fields in a search are sorted last

    • To update a specific entry of mutiple entries
      db.people.updateOne(
      { _id: ObjectId("123"), "people.name": "Mark" },
      { $set: { "people.$.children": 20 } }
      )

    • To do a text search : db.collection.find({$text: {$search: "search_query"}})

  • Aggregation

    • Links

      • https://www.mongodb.com/docs/upcoming/aggregation/

      • Tutorial : https://studio3t.com/knowledge-base/articles/mongodb-aggregation-framework/

    • Aggregation

      • Is a list of commands where a commands output is fed to the next command in line

    • To limit fields in an aggregation pipeline, use projection.

    • $all : compares array to given array, if the array has all elements of given array

    • $in : If array contains an element in given array

    • $elematch : returns document if field matches. Works with subdocuments.

      • Ex: db.people.find( {children : { $elematch : { type : “son” } } } )

    • Aggregation Framework each individual pipeline stage has a limit of 200 MB

    • TODO: Make article showing aggregation pipeline with simple data and commands.

      • Starting : aggregation.txt

    • OLD not readable to newbies article : http://www.menprojects.com/public/mongo_queries.html

  • Indexes

    • MongoDB recommends following the ESR (Equality, Search, Range)

      • equality, sort order, range filters

      • Indexes are B-tree

      • A field with numeric and non-numeric, it sorted lexicographical

      • Types: regular , spatial, text

  • Replica set

    • Add replica : Connect to the primary member, add the new member to the replica set configuration, initiate a resync on the new member, and wait for the new member to become a secondary.

    • Host Ids in replica set must be unique. And host:port as well.

    • Write Concern is the no of secondaries needs to return write ack.

    • To add a hidden member : rs.add(hostname, {priority: 0, hidden: true})

      • You want it hidden and no possibility of failover.

    • Backup server : {“priority : 0, :hidden” : true , “buildIndexes” : false}

  • Shards

    • Meta is stored on config servers

    • Covered queries are in a single shard.

    • To migrate data from one shard to another, use MongoDB's tool
      or https://www.mongodb.com/docs/manual/tutorial/migrate-sharded-cluster-to-new-hardware/

    • Choose shard key that is unique and has a large no of distinct values. Shard key can be multi-keys.

  • Security

    • For high security : Role based accounts, secure LDAP, full disk encryption, secure key management, and SSL

  • Explain

    • db.people.find({age: {$gt: 30}}).explain("executionStats")

    • https://www.mongodb.com/docs/manual/reference/explain-results/

    • Terms :

      • COLLSCAN : full collection scan

      • IXSCAN : index scan

      • FETCH L : retrieving documents

      • GROUP : grouping documents

      • SHARD_MERGE

      • SHARDING_FILTER

      • BATCHED_DELETE

  • Slow Log

    • Use Profile to Identify queries : https://www.mongodb.com/docs/manual/tutorial/manage-the-database-profiler/

    • db.setProfilingLevel(1, { slowms: 20 })

    • db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()

  • Location of uncommitted queries is replica failover, rollback:

    • “The createRollbackDataFiles parameter controls whether or not rollback files are created during rollbacks.”

    • After 4.4, its used the uid od the collection

    • “<dbpath>/rollback/20f74796-d5ea-42f5-8c95-f79b39bad190/removed.2020-02-19T04-57-11.0.bson”

    • on the former primary

  • Upgrades

    • Check compatibility issues and if set
      db.adminCommand( { setFeatureCompatibilityVersion: "6.0" } )

    • For replica sets and shardsUpgrade secondaries one at a time, wait for members to catch up or he primary, then upgrade the primary

    • When upgrading the replica sets, make sure applications are compatible with the upgrade.

  • Text Searches

    • You must have a text index.

    • To perform text searches, use $text operator

    • https://www.mongodb.com/docs/manual/reference/operator/query/text/

  • Schema Validation

    • https://www.mongodb.com/docs/manual/core/schema-validation/

  • Status and stats commands

    • db.collection.stats(<option>) returns statistics about the collection.

    • db.stats()

    • rs.status – replica status

    • db.runCommand({ serverStatus: 1}) – server status. Includes replica status, and other things. Contains most information from status commands.
      Or db.serverStatus()

    • show dbs

    • show collections

    • sh.status() - shard status

  • Changes

    • Mongodb 6 : https://www.mongodb.com/docs/manual/release-notes/6.0/

    • MongoDB 7 : https://www.mongodb.com/docs/v7.0/release-notes/7.0/

  • CheatSheet

    • https://www.mongodb.com/developer/products/mongodb/cheat-sheet/

    • Amazon : https://mongodb-devhub-cms.s3.us-west-1.amazonaws.com/Mongo_DB_Shell_Cheat_Sheet_1a0e3aa962.pdf

    • Interviewbit : https://www.interviewbit.com/mongodb-cheat-sheet/

  • New Stuff

    • Time Series : https://www.mongodb.com/developer/products/mongodb/new-time-series-collections/

  • Misc

    • maximum size of a MongoDB document : 16 mb

  • Data Migration (TODO)

    • AWS

    • AzureComos

    • GCP

  • Engines

    • WiredTiger

      • document level concurrency.

      • With 4 shard ACID

      • compression with ZSTD starting in 4.2

    • MMAPv1 was depreciated in 4.2