3、MongoDB基本管理命令
来源:互联网 发布:人工智能大会唐平中 编辑:程序博客网 时间:2024/05/22 14:02
目录(?)[+]
MongoDB是一个NoSQL数据库系统:一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表;而每个集合中可以存储一组由列标识的记录,列是可以自由定义的,非常灵活,由一组列标识的实体的集合对应于关系数据库表中的行。下面通过熟悉MongoDB的基本管理命令,来了解MongoDB提供的DBMS的基本功能和行为。
MongoDB命令帮助系统
在安装MongoDB后,启动服务器进程(mongod),可以通过mongo命令(前提mongo.exe配置到根目录中)实现对MongoDB的管理和监控。看一下MongoDB的命令帮助系统:
- root@dev2:~# mongo
- MongoDB shell version: 1.8.3
- connecting to: test
- > help
- db.help() help on db methods
- db.mycoll.help() help on collection methods
- rs.help() help on replica set methods
- help connect connecting to a db help
- help admin administrative help
- help misc misc things to know
- help mr mapreduce help
- show dbs show database names
- show collections show collections in current database
- show users show users in current database
- show profile show most recent system.profile entries with time >= 1ms
- use <db_name> set current database
- db.foo.find() list objects in collection foo
- db.foo.find( { a : 1 } ) list objects in foo where a == 1
- it result of the last line evaluated; use to further iterate
- DBQuery.shellBatchSize = x set default number of items to display on shell
- exit quit the mongo shell
- > db.help()
- DB methods:
- db.addUser(username, password[, readOnly=false])
- db.auth(username, password) //如果这个db加了身份,就要用这个身份认证
- db.cloneDatabase(fromhost)
- db.commandHelp(name) returns the help for the command
- db.copyDatabase(fromdb, todb, fromhost)
- db.createCollection(name, { size : ..., capped : ..., max : ... } )
- db.currentOp() displays the current operation in the db
- db.dropDatabase()
- db.eval(func, args) run code server-side
- db.getCollection(cname) same as db['cname'] or db.cname
- db.getCollectionNames()
- db.getLastError() - just returns the err msg string
- db.getLastErrorObj() - return full status object
- db.getMongo() get the server connection object
- db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
- db.getName()
- db.getPrevError()
- db.getProfilingLevel() - deprecated
- db.getProfilingStatus() - returns if profiling is on and slow threshold
- db.getReplicationInfo()
- db.getSiblingDB(name) get the db at the same server as this one
- db.isMaster() check replica primary status
- db.killOp(opid) kills the current operation in the db
- db.listCommands() lists all the db commands
- db.printCollectionStats()
- db.printReplicationInfo()
- db.printSlaveReplicationInfo()
- db.printShardingStatus()
- db.removeUser(username)
- db.repairDatabase()
- db.resetError()
- db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }
- db.serverStatus()
- db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
- db.shutdownServer()
- db.stats()
- db.version() current version of the server
- db.getMongo().setSlaveOk() allow queries on a replication slave server
另一个比较基础的是对指定数据库的集合进行操作、管理和监控,可以通过查询db.mycoll.help()获取到:
- > db.mycoll.help()
- DBCollection help
- db.mycoll.find().help() - show DBCursor help
- db.mycoll.count()
- db.mycoll.dataSize()
- db.mycoll.distinct( key ) - eg. db.mycoll.distinct( 'x' )
- db.mycoll.drop() drop the collection
- db.mycoll.dropIndex(name)
- db.mycoll.dropIndexes()
- db.mycoll.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
- db.mycoll.reIndex()
- db.mycoll.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
- e.g. db.mycoll.find( {x:77} , {name:1, x:1} )
- db.mycoll.find(...).count()
- db.mycoll.find(...).limit(n)
- db.mycoll.find(...).skip(n)
- db.mycoll.find(...).sort(...)
- db.mycoll.findOne([query])
- db.mycoll.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
- db.mycoll.getDB() get DB object associated with collection
- db.mycoll.getIndexes()
- db.mycoll.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
- db.mycoll.mapReduce( mapFunction , reduceFunction , <optional params> )
- db.mycoll.remove(query)
- db.mycoll.renameCollection( newName , <dropTarget> ) renames the collection.
- db.mycoll.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
- db.mycoll.save(obj)
- db.mycoll.stats()
- db.mycoll.storageSize() - includes free space allocated to this collection
- db.mycoll.totalIndexSize() - size in bytes of all the indexes
- db.mycoll.totalSize() - storage allocated for all data and indexes
- db.mycoll.update(query, object[, upsert_bool, multi_bool])
- db.mycoll.validate() - SLOW
- db.mycoll.getShardVersion() - only for use with sharding
有关数据库和集合管理的相关命令,是最基础和最常用的,如集合查询、索引操作等。
基本命令及实例
下面通过实际的例子来演示一些常见的命令:
(一)基本命令
1、show dbs
- {
- "host" : "dev2",
- "version" : "1.8.3",
- "process" : "mongod",
- "uptime" : 845446,
- "uptimeEstimate" : 839192,
- "localTime" : ISODate("2011-12-27T04:03:12.512Z"),
- "globalLock" : {
- "totalTime" : 845445636925,
- "lockTime" : 13630973982,
- "ratio" : 0.016122827283818857,
- "currentQueue" : {
- "total" : 0,
- "readers" : 0,
- "writers" : 0
- },
- "activeClients" : {
- "total" : 0,
- "readers" : 0,
- "writers" : 0
- }
- },
- "mem" : {
- "bits" : 64,
- "resident" : 12208,
- "virtual" : 466785,
- "supported" : true,
- "mapped" : 466139
- },
- "connections" : {
- "current" : 27,
- "available" : 792
- },
- "extra_info" : {
- "note" : "fields vary by platform",
- "heap_usage_bytes" : 70895216,
- "page_faults" : 17213898
- },
- "indexCounters" : {
- "btree" : {
- "accesses" : 4466653,
- "hits" : 4465526,
- "misses" : 1127,
- "resets" : 0,
- "missRatio" : 0.00025231420484197006
- }
- },
- "backgroundFlushing" : {
- "flushes" : 14090,
- "total_ms" : 15204393,
- "average_ms" : 1079.0910574875797,
- "last_ms" : 669,
- "last_finished" : ISODate("2011-12-27T04:02:28.713Z")
- },
- "cursors" : {
- "totalOpen" : 3,
- "clientCursors_size" : 3,
- "timedOut" : 53
- },
- "network" : {
- "bytesIn" : 63460818650,
- "bytesOut" : 763926196104,
- "numRequests" : 67055921
- },
- "opcounters" : {
- "insert" : 7947057,
- "query" : 35720451,
- "update" : 16263239,
- "delete" : 154,
- "getmore" : 91707,
- "command" : 68520
- },
- "asserts" : {
- "regular" : 0,
- "warning" : 1,
- "msg" : 0,
- "user" : 7063866,
- "rollovers" : 0
- },
- "writeBacksQueued" : false,
- "ok" : 1
- }
- > db.stats()
- {
- "db" : "fragment",
- "collections" : 12,
- "objects" : 384553,
- "avgObjSize" : 3028.40198360174,
- "dataSize" : 1164581068,
- "storageSize" : 1328351744,
- "numExtents" : 109,
- "indexes" : 10,
- "indexSize" : 16072704,
- "fileSize" : 4226809856,
- "ok" : 1
- }
- > db.getCollectionNames()
- [
- "17u",
- "baseSe",
- "bytravel",
- "daodao",
- "go2eu",
- "lotour",
- "lvping",
- "mafengwo",
- "sina",
- "sohu",
- "system.indexes"
- ]
(二)基本DDL和DML
- > show dbs
- admin 0.03125GB
- local (empty)
- pagedb 0.03125GB
- test 0.03125GB
- > use LuceneIndexDB
- switched to db LuceneIndexDB
- > show dbs
- admin 0.03125GB
- local (empty)
- pagedb 0.03125GB
- test 0.03125GB
- > db
- LuceneIndexDB
- > db.storeCollection.save({'version':'3.5', 'segment':'e3ol6'})
- > show dbs
- LuceneIndexDB 0.03125GB
- admin 0.03125GB
- local (empty)
- pagedb 0.03125GB
- test 0.03125GB
- >
- > db.createCollection('replicationColletion', {'capped':true, 'size':10240, 'max':17855200})
- { "ok" : 1 }
- > show collections
- replicationColletion
- storeCollection
- system.indexes
4、删除集合
删除集合,可以执行db.mycoll.drop()。
5、插入更新记录
直接使用集合的save方法,如下所示:
- > <em>db.storeCollection.save({'version':'3.5', 'segment':'e3ol6'})</em>
更新记录,使用save会将原来的记录值进行覆盖实现记录更新。
6、查询一条记录
使用findOne()函数,参数为查询条件,可选,系统会随机查询获取到满足条件的一条记录(如果存在查询结果数量大于等于1)示例如下所示:
- > db.storeCollection.findOne({'version':'3.5'})
- {
- "_id" : ObjectId("4ef970f23c1fc4613425accc"),
- "version" : "3.5",
- "segment" : "e3ol6"
- }
使用find()函数,参数指定查询条件,不指定条件则查询全部记录。
8、删除记录
使用集合的remove()方法,参数指定为查询条件,示例如下所示:
- > db.storeCollection.remove({'version':'3.5'})
- > db.storeCollection.findOne()
- null
可以使用集合的ensureIndex(keypattern[,options])方法,示例如下所示:
- > use pagedb
- switched to db pagedb
- > db.page.ensureIndex({'title':1, 'url':-1})
- > db.system.indexes.find()
- { "name" : "_id_", "ns" : "pagedb.page", "key" : { "_id" : 1 }, "v" : 0 }
- { "name" : "_id_", "ns" : "pagedb.system.users", "key" : { "_id" : 1 }, "v" : 0}
- { "_id" : ObjectId("4ef977633c1fc4613425accd"), "ns" : "pagedb.page", "key" : {"title" : 1, "url" : -1 }, "name" : "title_1_url_-1", "v" : 0 }
使用db.system.indexes.find()可以查询全部索引。
10、查询索引
我们为集合建立的索引,那么可以通过集合的getIndexes()方法实现查询,示例如下所示:
- > db.page.getIndexes()
- [
- {
- "name" : "_id_",
- "ns" : "pagedb.page",
- "key" : {
- "_id" : 1
- },
- "v" : 0
- },
- {
- "_id" : ObjectId("4ef977633c1fc4613425accd"),
- "ns" : "pagedb.page",
- "key" : {
- "title" : 1,
- "url" : -1
- },
- "name" : "title_1_url_-1",
- "v" : 0
- }
- ]
11、删除索引
删除索引给出了两个方法:
- db.mycoll.dropIndex(name)
- db.mycoll.dropIndexes()
12、索引重建
可以通过集合的reIndex()方法进行索引的重建,示例如下所示:
- > db.page.reIndex()
- {
- "nIndexesWas" : 2,
- "msg" : "indexes dropped for collection",
- "ok" : 1,
- "nIndexes" : 2,
- "indexes" : [
- {
- "name" : "_id_",
- "ns" : "pagedb.page",
- "key" : {
- "_id" : 1
- },
- "v" : 0
- },
- {
- "_id" : ObjectId("4ef977633c1fc4613425accd"),
- "ns" : "pagedb.page",
- "key" : {
- "title" : 1,
- "url" : -1
- },
- "name" : "title_1_url_-1",
- "v" : 0
- }
- ],
- "ok" : 1
- }
13、统计集合记录数
use fragment
db.baseSe.count()
统计结果,如下所示:
- > use fragment
- switched to db fragment
- > db.baseSe.count()
- 36749
14、查询并统计结果记录数
use fragment
db.baseSe.find().count()
find()可以提供查询参数,然后查询并统计结果,如下所示:
- > use fragment
- switched to db fragment
- > db.baseSe.find().count()
- 36749
15、查询指定数据库的集合当前可用的存储空间
use fragment
> db.baseSe.storageSize()
142564096
16、查询指定数据库的集合分配的存储空间
> db.baseSe.totalSize()
144096000
上述查询结果中,包括为集合(数据及其索引存储)分配的存储空间。
(三)启动与终止
(四)安全管理
或者,也可以修改/etc/mongodb.conf,设置auth=true,重启mongod进程。
- {
- "user" : "admin",
- "readOnly" : false,
- "pwd" : "995d2143e0bf79cba24b58b3e41852cd"
- }
- db.system.users.find()
- { "_id" : ObjectId("4ef940a13c1fc4613425acc8"), "user" : "admin", "readOnly" : false, "pwd" : "995d2143e0bf79cba24b58b3e41852cd" }
- db.system.users.find()
- error: {
- "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1", "code" : 10057
- }
说明:
- {
- "info" : "now locked against writes, use db.$cmd.sys.unlock.findOne() to unlock",
- "ok" : 1
- }
- {
- "inprog" : [ ],
- "fsyncLock" : 1,
- "info" : "use db.$cmd.sys.unlock.findOne() to terminate the fsync write/snapshot lock"
- }
db.$cmd.sys.unlock.findOne()
- { "ok" : 1, "info" : "unlock requested" }
db.currentOp()
状态信息如下:
- { "inprog" : [ ] }
(五)数据备份、恢复与迁移管理
cd testbak
mongodump
2、备份指定数据库
mongodump -d pagedb
mongorestore --drop
mongorestore -d pagedb --drop
说明:将备份的pagedb的数据恢复到数据库。
mongorestore -d pagedb -c page --drop
说明:将备份的pagedb的的page集合的数据恢复到数据库。
--type支持的类型有三个:csv、tsv、json
其他各个选项的使用,可以查看帮助:
- mongoimport --help
- options:
- --help produce help message
- -v [ --verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- -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
- --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 if dbpath specified, each db is in a separate
- directory
- -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
- --ignoreBlanks if given, empty fields in csv and tsv will be ignored
- --type arg type of file to import. default: json (json,csv,tsv)
- --file arg file to import from; if not specified stdin is used
- --drop drop collection first
- --headerline CSV,TSV only - use first line as headers
- --upsert insert or update objects that already exist
- --upsertFields arg comma-separated fields for the query part of the
- upsert. You should make sure this is indexed
- --stopOnError stop importing at first error rather than continuing
- --jsonArray load a json array, not one item per line. Currently
- limited to 4MB.
说明:将pagedb数据库中page集合的数据导出到pages.csv文件,其中各选项含义:
-f 指定cvs列名为_id,title,url,spiderName,pubDate
-q 指定查询条件
其他各个选项的使用,可以查看帮助:
- mongoexport --help
- options:
- --help produce help message
- -v [ --verbose ] be more verbose (include multiple times for more verbosity e.g. -vvvvv)
- -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
- --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 if dbpath specified, each db is in a separate directory
- -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
- mongoexport -d page -c Article -q '{"spiderName": "mafengwoSpider"}' -f _id,title,content,images,publishDate,spiderName,url --jsonArray > mafengwoArticle.txt
- ERROR: too many positional options
(六)远程连接管理
- mongo -u admin -p admin 192.168.0.197:27017/pagedb
- mongo --help (命令行键入)
- MongoDB shell version: 1.8.3
- usage: mongo [options] [db address] [file names (ending in .js)]
- db address can be:
- foo foo database on local machine
- 192.169.0.5/foo foo database on 192.168.0.5 machine
- 192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
- options:
- --shell run the shell after executing files
- --nodb don't connect to mongod on startup - no 'db address'
- arg expected
- --quiet be less chatty
- --port arg port to connect to
- --host arg server to connect to
- --eval arg evaluate javascript
- -u [ --username ] arg username for authentication
- -p [ --password ] arg password for authentication
- -h [ --help ] show this usage information
- --version show version information
- --verbose increase verbosity
- --ipv6 enable IPv6 support (disabled by default)
shell客户端api参考:
mongo –path
db.AddUser(username,password) 添加用户
db.auth(usrename,password) 设置数据库连接验证
db.cloneDataBase(fromhost) 从目标服务器克隆一个数据库
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 复制数据库fromdb—源数据库名称,todb—目标数据库名称,fromhost—源数据库服务器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 创建一个数据集,相当于一个表
db.currentOp() 取消当前库的当前操作
db.dropDataBase() 删除当前数据库
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一个数据集合,同用法:db['cname'] or db.cname
db.getCollenctionNames() 取得所有数据集合的名称列表
db.getLastError() 返回最后一个错误的提示消息
db.getLastErrorObj() 返回最后一个错误的对象
db.getMongo() 取得当前服务器的连接对象get the server connection object
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回当操作数据库的名称
db.getPrevError() 返回上一个错误对象
db.getProfilingLevel() ?什么等级
db.getReplicationInfo() ?什么信息
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(杀死)在当前库的当前操作
db.printCollectionStats() 返回当前库的数据集状态
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回当前数据库是否为共享数据库
db.removeUser(username) 删除用户
db.repairDatabase() 修复当前数据库
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 关闭当前服务程序
db.version() 返回当前程序的版本信息db.linlin.find({id:10}) 返回linlin数据集ID=10的数据集
db.linlin.find({id:10}).count() 返回linlin数据集ID=10的数据总数
db.linlin.find({id:10}).limit(2)返回linlin数据集ID=10的数据集从第二条开始的数据集
db.linlin.find({id:10}).skip(8) 返回linlin数据集ID=10的数据集从0到第八条的数据集
db.linlin.find({id:10}).limit(2).skip(8) 返回linlin数据集ID=1=的数据集从第二条到第八条的数据
db.linlin.find({id:10}).sort() 返回linlin数据集ID=10的排序数据集
db.linlin.findOne([query]) 返回符合条件的一条数据
db.linlin.getDB() 返回此数据集所属的数据库名称
db.linlin.getIndexes() 返回些数据集的索引信息
db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})
db.linlin.mapReduce(mayFunction,reduceFunction,
)
db.linlin.remove(query) 在数据集中删除一条数据
db.linlin.renameCollection(newName) 重命名些数据集名称
db.linlin.save(obj) 往数据集中插入一条数据
db.linlin.stats() 返回此数据集的状态
db.linlin.storageSize() 返回此数据集的存储大小
db.linlin.totalIndexSize() 返回此数据集的索引文件大小
db.linlin.totalSize() 返回些数据集的总大小
db.linlin.update(query,object[,upsert_bool])在此数据集中更新一条数据
db.linlin.validate() 验证此数据集
db.linlin.getShardVersion() 返回数据集共享版本号
db.linlin.find({‘name’:'foobar’}) select * from linlin where name=’foobar’
db.linlin.find() select * from linlin
db.linlin.find({‘ID’:10}).count() select count(*) from linlin where ID=10
db.linlin.find().skip(10).limit(20) 从查询结果的第十条开始读20条数据 select * from linlin limit 10,20 ———-mysql
db.linlin.find({‘ID’:{$in:[25,35,45]}}) select * from linlin where ID in (25,35,45)
db.linlin.find().sort({‘ID’:-1}) select * from linlin order by ID desc
db.linlin.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name) from linlin where ID<20
db.linlin.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})
select name,sum(marks) from linlin group by name
db.linlin.find('this.ID<20′,{name:1}) select name from linlin where ID<20
db.linlin.insert({'name':'foobar’,'age':25}) insert into linlin ('name','age’)values('foobar',25)
db.linlin.insert({'name':'foobar’,'age':25,’email’:'cclove2@163.com’})
db.linlin.remove({}) delete * from linlin
db.linlin.remove({'age':20}) delete linlin where age=20
db.linlin.remove({'age':{$lt:20}}) delete linlin where age<20
db.linlin.remove({'age':{$lte:20}}) delete linlin where age<=20
db.linlin.remove({'age':{$gt:20}}) delete linlin where age>20
db.linlin.remove({‘age’:{$gte:20}}) delete linlin where age>=20
db.linlin.remove({‘age’:{$ne:20}}) delete linlin where age!=20
db.linlin.update({‘name’:'foobar’},{‘$set’:{‘age’:36}}) update linlin set age=36 where name=’foobar’
db.linlin.update({‘name’:'foobar’},{‘$inc’:{‘age’:3}}) update linlin set age=age+3 where name=’foobar’
- 3、MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令 .
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- MongoDB基本管理命令
- 如何判断服务器返回的图片类型为GIF
- C++作业3
- 基础 HTML之目录问题(相对路径和绝对路径区别)
- HTML JS HTTP 精品插件记录
- 100个常用的 PHP 类库、资源和技巧小结
- 3、MongoDB基本管理命令
- 在Xshell中使用vim乱码解决方法
- wifidog 源码初分析(1)
- 关于tableview的headerview遮挡住cell的问题
- socket客户端发送消息到服务器端简单示例
- 同上个问题
- 为什么你总成为不了架构师?
- leetcode197-Rising Temperature
- Java 朴素版计算器实现(代码冗长)