all:bibles:langages:mongodb
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| all:bibles:langages:mongodb [2024/11/19 08:21] – [Exécution de commandes] omeylhoc | all:bibles:langages:mongodb [2025/09/22 11:11] (Version actuelle) – [Identification] omeylhoc | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== MongoDB ====== | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Installation ===== | ||
| + | |||
| + | ==== Installation Redhat 9 ==== | ||
| + | |||
| + | === Configuration dépôt === | ||
| + | |||
| + | Créer le fichier <color # | ||
| + | |||
| + | <code bash> | ||
| + | [mongodb-org-x.0] | ||
| + | name=MongoDB Repository | ||
| + | baseurl=https:// | ||
| + | gpgcheck=1 | ||
| + | enabled=1 | ||
| + | gpgkey=https:// | ||
| + | </ | ||
| + | |||
| + | === Installation === | ||
| + | |||
| + | <code bash> | ||
| + | sudo dnf install mongodb-org | ||
| + | </ | ||
| + | |||
| + | ==== Configuration ==== | ||
| + | |||
| + | Paramétrage dans le fichier <color # | ||
| + | |||
| + | === Interface et port d' | ||
| + | |||
| + | <code bash> | ||
| + | net: | ||
| + | port: 27017 # port d' | ||
| + | bindIp: 127.0.0.1 | ||
| + | </ | ||
| + | |||
| + | === Activation TLS === | ||
| + | |||
| + | <code bash> | ||
| + | net: | ||
| + | ... | ||
| + | tls: | ||
| + | mode: requireTLS | ||
| + | certificateKeyFile: | ||
| + | CAFile: / | ||
| + | </ | ||
| + | |||
| + | ==== Fichiers ==== | ||
| + | |||
| + | * ~/.dbshell : historique des commandes | ||
| + | * ~/ | ||
| + | * / | ||
| + | |||
| + | ===== Accès ===== | ||
| + | |||
| + | ==== Identification ==== | ||
| + | |||
| + | === Accès sans mot de passe === | ||
| + | |||
| + | <code bash> | ||
| + | mongosh -u < | ||
| + | </ | ||
| + | |||
| + | === Accès avec mot de passe === | ||
| + | |||
| + | <code bash> | ||
| + | mongosh -u < | ||
| + | mongo 10.1.1.189: | ||
| + | mongo --host 10.1.1.189 --port 27017 -u mongoadmin -p M0ng0@dmin --authenticationDatabase admin | ||
| + | </ | ||
| + | |||
| + | === Connexion en TLS === | ||
| + | |||
| + | <code bash> | ||
| + | sudo mongosh 127.0.0.1: | ||
| + | </ | ||
| + | |||
| + | ==== Réinitialiser utilisateur ==== | ||
| + | |||
| + | <code bash> | ||
| + | sudo systemctl stop mongod | ||
| + | sudo mongod --dbpath / | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | mongosh --port 27017 | ||
| + | </ | ||
| + | |||
| + | <code sql> | ||
| + | use admin | ||
| + | db.system.users.find().pretty() | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | use admin | ||
| + | db.dropUser(" | ||
| + | db.dropUser(" | ||
| + | db.createUser( | ||
| + | { | ||
| + | user: " | ||
| + | pwd: " | ||
| + | roles: [ { role: " | ||
| + | } | ||
| + | ) | ||
| + | db.createUser( | ||
| + | { | ||
| + | user: " | ||
| + | pwd: " | ||
| + | roles: [ { role: " | ||
| + | } | ||
| + | ) | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | sudo chown -R mongod: / | ||
| + | sudo systemctl start mongod | ||
| + | mongo < mongo.script | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Commandes de base ===== | ||
| + | |||
| + | ==== Lister les bases de données ==== | ||
| + | |||
| + | <code mysql> | ||
| + | show dbs | ||
| + | use <db> | ||
| + | show tables | ||
| + | </ | ||
| + | |||
| + | === findOne === | ||
| + | |||
| + | Il n'y a pas de structure à proprement parlé dans mongo mais il est possible d' | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | === find === | ||
| + | |||
| + | * Affiche tous les enregistrements d'une table | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | * Pour compter le nombre d' | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | * Pour afficher tous les enregistrements trouvés (et non pas les 20 premiers) | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | db.< | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | * Affiche certains champs sous condition | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | Affiche le champ " | ||
| + | |||
| + | * Liste les enregistrements dont champ.souschamp a une valeur donnée | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | * Liste les enregistrements dont champ.souschamp est égal à toto | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | === update === | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | db.users.updateOne({ " | ||
| + | db.< | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | * **$set** | ||
| + | * **$unset** | ||
| + | * **$inc** | ||
| + | * **$push** | ||
| + | * **$pull** | ||
| + | * **$rename** : Renomme un champ | ||
| + | |||
| + | <code mysql> | ||
| + | db.< | ||
| + | db.< | ||
| + | </ | ||
| + | |||
| + | ==== Exécution de commandes ==== | ||
| + | |||
| + | === Exécution d'une commande depuis le shell === | ||
| + | |||
| + | <code bash> | ||
| + | mongo --host <@IP> --port < | ||
| + | </ | ||
| + | |||
| + | === Exécution d'un fichier de commande === | ||
| + | |||
| + | <code bash> | ||
| + | mongo --host <@IP> --port < | ||
| + | </ | ||
| + | |||
| + | === Options === | ||
| + | |||
| + | * < | ||
| + | * < | ||
| + | |||
| + | ==== Résultat dans un fichier ==== | ||
| + | |||
| + | * Redirection shell simple | ||
| + | |||
| + | <code bash> | ||
| + | mongo --host <@IP> --port < | ||
| + | </ | ||
| + | |||
| + | * fichier CSV avec mongoexport | ||
| + | |||
| + | <code bash> | ||
| + | mongoexport --host <@IP> --port < | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Manipulation bdd ===== | ||
| + | |||
| + | ==== Backup/ | ||
| + | |||
| + | === Exporter une base de données === | ||
| + | |||
| + | <code bash> | ||
| + | mongodump --host=< | ||
| + | </ | ||
| + | |||
| + | ---- | ||
