MongoDb: Difference between revisions

From Objectif Client Inc
Jump to navigation Jump to search
Line 125: Line 125:
db.setLogLevel(2, 'query')
db.setLogLevel(2, 'query')
</syntaxhighlight>
</syntaxhighlight>
</div>
</div>


* Replication
 
==== Replication ====
* Config
<syntaxhighlight lang="JavaScript">
<syntaxhighlight lang="JavaScript">
config = {_id: 'rep_01', members: [
config = {_id: 'rep_01', members: [
Line 136: Line 136:
         };
         };
</syntaxhighlight>
</syntaxhighlight>
* Initiate Replication
<syntaxhighlight lang="JavaScript">
rs.initiate(config)
</syntaxhighlight>
* Add another machine
<syntaxhighlight lang="JavaScript">
rs.add
</syntaxhighlight>
* Replication configuration / status
<syntaxhighlight lang="JavaScript">
rs.conf()
rs.status()
</syntaxhighlight>
</div>
</div>


== Http Interface ==
== Http Interface ==

Revision as of 04:14, 8 June 2020

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().limit(x)
db.collection.find().limit(x).pretty
  • Retreive creation date of the document
ObjectId("xxxxxxxxxxxxxxxx").getTimestamp()
ISODATE("2020-06-06T17:33:42Z")

Database Engine Management

  • Stop Database
db.adminCommand( { shutdown: 1} )
  • Manage Log Information
db.getLogComponents
db.setLogLevel(2, 'query')


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