MongoDb

From Objectif Client Inc
Jump to navigation Jump to search

Cheat sheet

Database Management

  • Connect to a mongodb
mongo
  • Display dabases
show dbs
  • Connect to a database
use database
  • List Tables (Collections)
show collections
  • Create Collection
use formation
db.cours.insert({"titre":"MongoDB 101"})
  • Remove Tables (Collections)
db.collection.drop()
  • Remove Database (Collections)
db.dropDatabase()
  • Statistics must be connected to database
db.stats()

User Management

  • Create Admin user
use admin
db.createUser(
   {
     user: "userName",
     pwd:  "passWord",
     roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
   }
)
  • Remove user
use admin
db.dropUser("userName")
  • Change user Password
db.changeUserPassword("username:", "newPassWord")
  • Login
mongo -u userName -p passWord --authenticationDatabase admin

or 

mongo admin -u 'userName' -p 'passWord'

or 
mongo
use admin
db.auth("userName", "passWord")

Backup/Restore Import/Export

  • Backup
mondodump -u userName -p passWord --authenticationDatabase admin
mondodump --db toDb --collection toCollection -u userName -p passWord --authenticationDatabase admin 
mondodump --db toDb --collection toCollection -u userName -p passWord --authenticationDatabase admin --oplog
  • Import
mongoimport --db toDb --collection toCollection --drop -u userName -p passWord --authenticationDatabase admin --file /home/userName/fileToImport.json

Read Data

  • Display content of a table
db.collection.find()
db.collection.find({("attributName" : "value" })
db.collection.find({("address.city" : "value" }).sort({"address.zipCode" : -1}).skip(5).limit(10)
db.collection.find({"country.city.population" : &lte 100000 }).sort({"country.city" : -1}).limit(10)
db.collection.find({"country.city.population" : &lte 100000, country : "Italia" }).sort({"country.city" : -1}).limit(10)
db.collection.find({$or: [{"country.city" : "Arles"},{"country.city" : "Avignon"})
db.collection.find({"country.city" : { $in: ["Arles","Avignon"]}})
db.collection.find({"country.city" : { $nin: ["Arles","Avignon"]}})
db.collection.find({"country.city" : /Saint/i})
db.collection.find({"country.city" : /$Saint/i})
db.collection.find().count()
db.collection.find().limit(x)
db.collection.find().limit(x).pretty
  • Joint
db.collection.aggregate {
   [ 
     {$loockup:
         { 
           from: "collection2",
           localField: "borough",
           foreignField: "_id",
           as "brough"
        }
     }
  ]
}
  • Complex Query
db.collection.aggregate {
   [ 
     {$match: { "city.population": { $gt: 100000} } },
     {$group: { _id: :$borough", total: { $sum:1} } },
     {$sort: {total: -1} }
   ]
}
  • Retreive creation date of the document
ObjectId("xxxxxxxxxxxxxxxx").getTimestamp()
ISODATE("2020-06-06T17:33:42Z")

Commit transaction

with client.start_session() as session 
session.start_transaction
collection.insert_one(collectionName, session = session)
session.commit_transaction or session.abort_transaction

Explain / Log Setup

  • Explain Query
db.collection.find({"country.city.population" : &lte 100000, country : "Italia" }).explain()
  • Manage Log Information
db.getLogComponents
db.setLogLevel(2, 'query')

Database Engine Management

  • Stop Database
db.adminCommand( { shutdown: 1} )

Replication

  • Config
config = {_id: 'rep_01', members: [
             {_id: 0, 'ipaddress:port'},
             {_id: 1, 'ipaddress:port'},
             {_id: 2, 'ipaddress:port'}]
         };
  • Initiate Replication
rs.initiate(config)
  • Add another machine
rs.add
  • Replication configuration / status
rs.conf()
rs.status()

Http Interface

Inable Http Interface

add http.enabled: true in /etc/mongod.conf

net:
  port: 27017
  bindIp: 127.0.0.1
  http.enabled: true
  http.RESTInterfaceEnabled: true

Access to htps interface

Http Interface

GUI Tool

  • Robo 3T