mongodb 常用语句(管理员操作)

 

用户操作需要进入admin数据库

use admin;

 

增加或修改一个用

db.addUser("market","market");(可写)

db.addUser("market","market",true);(只读)

 

查看所有用户

show users;

 

删除一个用户

db.removeUser("market");

 

一、数据库备份

F:\>mongodump  -h 192.168.1.72:27017 -d market -o f:\mongodb\market

n1.从192.168.1.72:27017上备份market数据库到目录f:\mongodb\market

n2.目录中的内容会被重建,默认会覆盖原数据文件

 

二、数据库还原

F:\>mongorestore -h 192.168.1.72:27017 -d market --directoryperdb f:\mongodb\market\market\

n1.把f:\mongodb\market\market\下的文件导入到192.168.1.72:27017的market数据库中

n2.market数据库会重建

 

三、数据库导出

F:\>mongoexport -h 192.168.1.72:27017 -d market -c app_search_key_2012-02-18 -o f:\mongodb\app_search_key_2012-02-18.json

n1.从192.168.1.72:27017上market数据库中导出表app_search_key_2012-02-18,数据放在文件f:\mongodb\app_search_key_2012-02-18.json中

n2.文件会重建

n3.导出json格式的内容,如果需要向mysql中转移数据库,需要导出csv格式的数据,如:

F:\>mongoexport -h 192.168.1.72:27017 -d market -c app_search_key_2012-02-18 -o f:\mongodb\app_search_key_2012-02-18.csv --csv -f key,date

 

四、数据库导入

F:\>mongoimport -h192.168.1.72:27017 -d market -c app_search_key_2012-02-18 --file f:\mongodb\app_search_key_2012-02-18.json

n1.导入文件f:\mongodb\app_search_key_2012-02-18.json到192.168.1.72:27017上的app_search_key_2012-02-18表

n2.导入csv格式的数据

F:\>mongoimport -h192.168.1.72:27017 -d market -c app_search_key_2012-02-18 --file f:\mongodb\app_search_key_2012-02-18.csv --type csv --headerline

n3.数据会追加

 

mongoexport -h 127.0.0.1 -d tmp -c cccc -q '{nsg:"de5"}' -o /tmp/dddd.csv -f a,v,f,mid,splus,message  --csv

root@tts-0001:/home/ttpod/cttpod# mongoexport --help

Export MongoDB data to CSV, TSV or JSON files.

 

options:

  --help                                produce help message

  -v [ --verbose ]                      be more verbose (include multiple times

                                        for more verbosity e.g. -vvvvv)

  --version                             print the program's version and exit

  -h [ --host ] arg                     mongo host to connect to ( <set 

                                        name>/s1,s2 for sets)

  --port arg                            server port. Can also use --host 

                                        hostname:port

  --ipv6                                enable IPv6 support (disabled by 

                                        default)

  -u [ --username ] arg                 username

  -p [ --password ] arg                 password

  --authenticationDatabase arg          user source (defaults to dbname)

  --authenticationMechanism arg (=MONGODB-CR)

                                        authentication mechanism

  --dbpath arg                          directly access mongod database files 

                                        in the given path, instead of 

                                        connecting to a mongod  server - needs 

                                        to lock the data directory, so cannot 

                                        be used if a mongod is currently 

                                        accessing the same path

  --directoryperdb                      each db is in a separate directly 

                                        (relevant only if dbpath specified)

  --journal                             enable journaling (relevant only if 

                                        dbpath specified)

  -d [ --db ] arg                       database to use

  -c [ --collection ] arg               collection to use (some commands)

  -f [ --fields ] arg                   comma separated list of field names 

                                        e.g. -f name,age

  --fieldFile arg                       file with fields names - 1 per line

  -q [ --query ] arg                    query filter, as a JSON string

  --csv                                 export to csv instead of json

  -o [ --out ] arg                      output file; if not specified, stdout 

                                        is used

  --jsonArray                           output to a json array rather than one 

                                        object per line

  -k [ --slaveOk ] arg (=1)             use secondaries for export if 

                                        available, default true

  --forceTableScan                      force a table scan (do not use 

                                        $snapshot)

猜你喜欢

转载自871421448.iteye.com/blog/1815527