Differences between revisions 6 and 16 (spanning 10 versions)
Revision 6 as of 2019-03-07 02:55:13
Size: 1834
Editor: PieterSmit
Comment:
Revision 16 as of 2020-04-12 07:03:38
Size: 2559
Editor: PieterSmit
Comment:
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
 *  * [[MongoDb/Performance]]
 * mongobackup + restore scripts [[MongoDb/MongoBackupRestore]]
 * https://hub.docker.com/r/mongoclient/mongoclient/
   * $ docker run -it mongoclient/mongoclient
   * $ docker run -it mongo mongoexport --version
Line 15: Line 19:


== Backup/Export ==
 * Use mongodump mongorestore for full bson that handles all mongo types.
 * To get json formatted data {{{
u=<user>
p=<password>
d=<mydb>
h-<host ip>
mongo --quiet "mongodb://$u:$p@$h/$d?authSource=admin" --eval "db.getCollectionNames().join('\n')" | \
grep -v system.indexes | \
xargs -L 1 -I {} mongoexport -u $u -p $p -h $h --db $d --authenticationDatabase admin --jsonArray --pretty --collection {} --out {}.json
}}}


== Basic mongo find and update examples ==
 * > db.getCollection('widget_data').find({"obj_type":"summary", "obj_category":"Widget", "obj_origin":"sample"}).pretty()
 * patch/update {{{
db.widget_data.update( {"obj_type":"summary", "obj_category":"Widget", "obj_origin":"sample"}, {$set: {
    "data_sets" : [
        {
            "rows" : [
                {
                    "col_1" : "Total",
                    "col_2" : "10"
                },
                {
                    "col_1" : "Limit",
                    "col_2" : "20"
                },
                {
                    "col_1" : "Max",
                    "col_2" : "50"
                }
            ]
        }
    ]

}}

)
}}}
Line 38: Line 84:
 === Slow / Long running queries ==
 * * $ mongo 10.32.2.4 --authenticationDatabase admin --username test --password mypass
 * > db.currentOp()
 * > db.currentOp({"secs_running": {$gte: 3}})

 * Count documents in <db> <collection>
   * use <db>;
   * db.<collection>.count()
Line 49: Line 87:
---- /!\ '''Edit conflict - other version:''' ----
Line 51: Line 88:
 === Slow / Long running queries ==
 * * $ mongo 10.32.2.4 --authenticationDatabase admin --username test --password mypass
 * > db.currentOp()
 * > db.currentOp({"secs_running": {$gte: 3}})

 * Count documents in <db> <collection>
   * use <db>;
   * db.<collection>.count()



---- /!\ '''Edit conflict - your version:''' ----

---- /!\ '''End of edit conflict''' ----

MongoDB nosql notes

Login

  • $ mongo 10.32.2.4 --authenticationDatabase admin --username test --password mypass
  • > db

    • test
  • > show dbs

  • > use 9spokes

  • > db.getCollection("")

  • > show collections

  • db.getCollection("config_data").find()

Backup/Export

  • Use mongodump mongorestore for full bson that handles all mongo types.
  • To get json formatted data

    u=<user>
    p=<password>
    d=<mydb>
    h-<host ip>
    mongo --quiet "mongodb://$u:$p@$h/$d?authSource=admin" --eval "db.getCollectionNames().join('\n')" | \
    grep -v system.indexes | \
    xargs -L 1 -I {} mongoexport -u $u -p $p -h $h --db $d --authenticationDatabase admin --jsonArray --pretty --collection {} --out {}.json

Basic mongo find and update examples

  • > db.getCollection('widget_data').find({"obj_type":"summary", "obj_category":"Widget", "obj_origin":"sample"}).pretty()

  • patch/update

    db.widget_data.update( {"obj_type":"summary", "obj_category":"Widget", "obj_origin":"sample"}, {$set: {
        "data_sets" : [ 
            {
                "rows" : [ 
                    {
                        "col_1" : "Total",
                        "col_2" : "10"
                    }, 
                    {
                        "col_1" : "Limit",
                        "col_2" : "20"
                    }, 
                    {
                        "col_1" : "Max",
                        "col_2" : "50"
                    }
                ]
            }
        ]
    
    }}
    
    )

Replicaset

  • View members
    •  mongo admin --host $ip -u root -p $pass  -eval "rs.status()" 

  • Check if current server is the write
    •  mongo admin --host $ip -u root -p $pass --quiet --eval "db.isMaster()['ismaster']"  

Recover password

  • stop the mongod service
  • edit /etc/mongod.conf
    •    :PRIMARY>  use admin
         :PRIMARY> db.createUser({user:"root",pwd:"mongopwd",roles:[{role:"root",db:"admin"}]});
         :PRIMARY> db.changeUserPassword("root","mongopwd")
    • mongo admin --host 192.168.70.13 -u root -p mongopassword --eval 'rs.initiate({_id: "local_sandbox-MongoDB", members: [{_id: 0, host: "192.168.70.11:27017"},{_id: 1, host: "192.168.70.12:27017"},{_id: 2, host: "192.168.70.13:27017"}]})'

...


CategoryLinux

MongoDb (last edited 2020-07-24 00:29:21 by PieterSmit)