MongoDb: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(38 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
== Cheat sheet == | == Cheat sheet == | ||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Database Management === | |||
<div class="mw-collapsible-content"> | |||
* Connect to a mongodb | * Connect to a mongodb | ||
<pre> | <pre>mongo</pre> | ||
* Display dabases | * Display dabases | ||
<pre>show dbs</pre> | <pre>show dbs</pre> | ||
* Connect to a database | * Connect to a database | ||
<pre>use database</pre> | <pre>use database</pre> | ||
* List Tables (Collections) | * List Tables (Collections) | ||
<pre>show collections</pre> | <pre>show collections</pre> | ||
* | |||
< | * Create Collection | ||
<syntaxhighlight lang="JavaScript"> | |||
use formation | |||
db.cours.insert({"titre":"MongoDB 101"}) | |||
</syntaxhighlight> | |||
* Remove Tables (Collections) | * Remove Tables (Collections) | ||
<pre>db.collection.drop()</pre> | <pre>db.collection.drop()</pre> | ||
* Remove Database (Collections) | * Remove Database (Collections) | ||
<pre>db.dropDatabase()</pre> | <pre>db.dropDatabase()</pre> | ||
* Statistics must be connected to database | |||
<syntaxhighlight lang="JavaScript"> | |||
db.stats() | |||
</syntaxhighlight> | |||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== User Management === | |||
<div class="mw-collapsible-content"> | |||
* Create Admin user | |||
<syntaxhighlight lang="JavaScript"> | |||
use admin | |||
db.createUser( | |||
{ | |||
user: "userName", | |||
pwd: "passWord", | |||
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] | |||
} | |||
) | |||
</syntaxhighlight> | |||
* Remove user | |||
<syntaxhighlight lang="JavaScript"> | |||
use admin | |||
db.dropUser("userName") | |||
</syntaxhighlight> | |||
* Change user Password | |||
<syntaxhighlight lang="JavaScript"> | |||
db.changeUserPassword("username:", "newPassWord") | |||
</syntaxhighlight> | |||
* Login | |||
<syntaxhighlight lang="JavaScript"> | |||
mongo -u userName -p passWord --authenticationDatabase admin | |||
or | |||
mongo admin -u 'userName' -p 'passWord' | |||
or | |||
mongo | |||
use admin | |||
db.auth("userName", "passWord") | |||
</syntaxhighlight> | |||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Backup/Restore Import/Export === | |||
<div class="mw-collapsible-content"> | |||
* Backup | |||
<syntaxhighlight lang="JavaScript"> | |||
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 | |||
</syntaxhighlight> | |||
* Import | |||
<syntaxhighlight lang="JavaScript"> | |||
mongoimport --db toDb --collection toCollection --drop -u userName -p passWord --authenticationDatabase admin --file /home/userName/fileToImport.json | |||
</syntaxhighlight> | |||
</div> | |||
</div> | |||
<div class="toccolours mw-collapsible mw-collapsed"> | |||
=== Read Data === | |||
<div class="mw-collapsible-content"> | |||
* Display content of a table | |||
<syntaxhighlight lang="JavaScript"> | |||
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 | |||
</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> | |||
* Retreive creation date of the document | |||
<syntaxhighlight lang="JavaScript"> | |||
ObjectId("xxxxxxxxxxxxxxxx").getTimestamp() | |||
ISODATE("2020-06-06T17:33:42Z") | |||
</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 === | |||
<div class="mw-collapsible-content"> | |||
* Stop Database | |||
<syntaxhighlight lang="JavaScript"> | |||
db.adminCommand( { shutdown: 1} ) | |||
</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 == | ||
=== Inable Http Interface === | |||
add http.enabled: true in /etc/mongod.conf | |||
<pre> | |||
net: | |||
port: 27017 | |||
bindIp: 127.0.0.1 | |||
http.enabled: true | |||
http.RESTInterfaceEnabled: true | |||
</pre> | |||
=== Access to htps interface === | |||
[http://localhost:28017/ Http Interface] | [http://localhost:28017/ Http Interface] | ||
== GUI Tool == | |||
* Robo 3T |
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