MongoDB

  • The MongoDB Shell
    // show all
    show dbs
    // show current
    db
    // use db
    use dbname
    // remove
    db.dropDatabase()
    // create table
    db.createCollection('name')
    // list
    show collections
  • CRUD Operations
    • Create / Insert
      await db.collection('inventory').insertOne({
        item: 'canvas',
        qty: 100,
        tags: ['cotton'],
        size: { h: 28, w: 35.5, uom: 'cm' }
      });
      
      await db.collection('inventory').insertMany([
        {
          item: 'journal',
          qty: 25,
          tags: ['blank', 'red'],
          size: { h: 14, w: 21, uom: 'cm' }
        },
        {
          item: 'mat',
          qty: 85,
          tags: ['gray'],
          size: { h: 27.9, w: 35.5, uom: 'cm' }
        },
        {
          item: 'mousepad',
          qty: 25,
          tags: ['gel', 'blue'],
          size: { h: 19, w: 22.85, uom: 'cm' }
        }
      ]);
    • Read / Query
      const cursor = db.collection('inventory').find({});
      
      const cursor = db.collection('inventory').find({
        status: { $in: ['A', 'D'] }
      });
      
      db.table.find().sort({name:1).pretty()
      db.table.find().count()
      db.table.find().limit(2)
      
      db.table.find().forEach(function(item){ print("item: " + item.name))})
      
      // only use part of return object
      db.table.find({name: 'a name'}, {key1:1, key3:1})
    • Update
      await db.collection('inventory').updateOne(
        { item: 'paper' },
        {
          $set: { 'size.uom': 'cm', status: 'P' },
          $currentDate: { lastModified: true }
        }
      );
      
      await db.collection('inventory').updateMany(
        { qty: { $lt: 50 } },
        {
          $set: { 'size.uom': 'in', status: 'P' },
          $currentDate: { lastModified: true }
        }
      );
    • Delete / Remove
      await db.collection('inventory').deleteOne({ status: 'D' });
      
      # delete all
      await db.collection('inventory').deleteMany({});
      
      
  • Examples
    db.players.updateOne(
    { _id: '69570212fbab23767af7e793' },
    { $set: { is_admin: true } });
    
    
    docker-compose exec mongodb \
    mongosh -u admin -p your_password --authenticationDatabase admin \
    cribbage_tournament \
    --eval 'db.players.updateOne({email: "newplayer@example.com"}, {$set: {is_admin: true}})'
mongodump --db dname --out `date '+%Y-%m-%d-mongo-'``hostname`

mongorestore --db dname --drop
mongorestore --host dev-set/10.1.5.32:27017 --db dname --drop
mongorestore --db dname --drop
mongorestore --host dev-set/10.1.5.32:27017 --db dname --drop path/to/files

mongo dname --eval 'db.systemProperties.update({"name" : "system-url"},{$set : {"value" : "https://HOST"}})'
time mongo dname --eval "db.runCommand( { repairDatabase: 1 } )"

# backup
mongodump --db dname --out $(date '+%Y-%m-%d-%H%M%S-dname')

# restore
mongorestore --db dname --drop
mongo dname --eval 'db.systemProperties.update({"name" : "system-url"},{$set : {"value" : "https://host.com"}})'