MongoDb: Difference between revisions
Jump to navigation
Jump to search
(21 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Cheat sheet == | == Cheat sheet == | ||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Database Management === | === Database Management === | ||
<div class="mw-collapsible-content"> | |||
* Connect to a mongodb | * Connect to a mongodb | ||
<pre>mongo</pre> | <pre>mongo</pre> | ||
Line 29: | Line 31: | ||
db.stats() | db.stats() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== User Management === | === User Management === | ||
<div class="mw-collapsible-content"> | |||
* Create Admin user | * Create Admin user | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
Line 68: | Line 74: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Backup/Restore Import/Export === | === Backup/Restore Import/Export === | ||
<div class="mw-collapsible-content"> | |||
* Backup | * Backup | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
mondodump -u userName -p passWord --authenticationDatabase admin | 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 | ||
mondodump --db toDb --collection toCollection -u userName -p passWord --authenticationDatabase admin --oplog | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 80: | Line 91: | ||
mongoimport --db toDb --collection toCollection --drop -u userName -p passWord --authenticationDatabase admin --file /home/userName/fileToImport.json | mongoimport --db toDb --collection toCollection --drop -u userName -p passWord --authenticationDatabase admin --file /home/userName/fileToImport.json | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Read Data === | === Read Data === | ||
<div class="mw-collapsible-content"> | |||
* Display content of a table | * Display content of a table | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
db.collection.find() | 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" : <e 100000 }).sort({"country.city" : -1}).limit(10) | |||
db.collection.find({"country.city.population" : <e 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) | ||
db.collection.find().limit(x).pretty | db.collection.find().limit(x).pretty | ||
</syntaxhighlight> | |||
* Joint | |||
<syntaxhighlight lang="JavaScript"> | |||
db.collection.aggregate { | |||
[ | |||
{$loockup: | |||
{ | |||
from: "collection2", | |||
localField: "borough", | |||
foreignField: "_id", | |||
as "brough" | |||
} | |||
} | |||
] | |||
} | |||
</syntaxhighlight> | |||
* Complex Query | |||
<syntaxhighlight lang="JavaScript"> | |||
db.collection.aggregate { | |||
[ | |||
{$match: { "city.population": { $gt: 100000} } }, | |||
{$group: { _id: :$borough", total: { $sum:1} } }, | |||
{$sort: {total: -1} } | |||
] | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 94: | Line 147: | ||
ISODATE("2020-06-06T17:33:42Z") | ISODATE("2020-06-06T17:33:42Z") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Commit transaction === | |||
<div class="mw-collapsible-content"> | |||
<syntaxhighlight lang="JavaScript"> | |||
with client.start_session() as session | |||
session.start_transaction | |||
collection.insert_one(collectionName, session = session) | |||
session.commit_transaction or session.abort_transaction | |||
</syntaxhighlight> | |||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Explain / Log Setup === | |||
<div class="mw-collapsible-content"> | |||
* Explain Query | |||
<syntaxhighlight lang="JavaScript"> | |||
db.collection.find({"country.city.population" : <e 100000, country : "Italia" }).explain() | |||
</syntaxhighlight> | |||
* Manage Log Information | |||
<syntaxhighlight lang="JavaScript"> | |||
db.getLogComponents | |||
db.setLogLevel(2, 'query') | |||
</syntaxhighlight> | |||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Database Engine Management === | === Database Engine Management === | ||
<div class="mw-collapsible-content"> | |||
* Stop Database | * Stop Database | ||
<syntaxhighlight lang="JavaScript"> | <syntaxhighlight lang="JavaScript"> | ||
db.adminCommand( { shutdown: 1} ) | db.adminCommand( { shutdown: 1} ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== Replication ==== | |||
* Config | |||
<syntaxhighlight lang="JavaScript"> | |||
config = {_id: 'rep_01', members: [ | |||
{_id: 0, 'ipaddress:port'}, | |||
{_id: 1, 'ipaddress:port'}, | |||
{_id: 2, 'ipaddress:port'}] | |||
}; | |||
</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 == |
Latest revision as of 03:20, 9 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({("attributName" : "value" })
db.collection.find({("address.city" : "value" }).sort({"address.zipCode" : -1}).skip(5).limit(10)
db.collection.find({"country.city.population" : <e 100000 }).sort({"country.city" : -1}).limit(10)
db.collection.find({"country.city.population" : <e 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" : <e 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
GUI Tool
- Robo 3T