手机版
你好,游客 登录 注册
背景:
阅读新闻

MongoDB数据库文档大全

[日期:2012-03-24] 来源:Linux社区  作者:wangkuifeng0118 [字体:大 中 小]

MongoDB的使用技巧

如果想查看当前连接在哪个数据 库下面,可以直接输入db

  1. > db
  2. Admin

想切换到test数据库

  1. > use test
  2. switched to db test
  3. > db
  4. Test

想 查看test下有哪些表或者叫collection,可以输入

  1. > show collections
  2. system.indexes
  3. user

想 知道mongodb支持哪些命令 ,可以直接输入help

  1. > help
  2. HELP
  3.         show dbs                     show database names
  4.         show collections             show collections in current database
  5.         show users                   show users in current database
  6.         show profile                 show most recent system.profile entries with time >= 1ms
  7.         use <db name>                set curent database to <db name>
  8.         db.help()                    help on DB methods
  9.         db.foo.help()                help on collection methods
  10.         db.foo.find()                list objects in collection foo
  11.         db.foo.find( { a : 1 } )     list objects in foo where a == 1
  12.         it                           result of the last line evaluated; use to further iterate

如果想知道当前数据库支持哪些方法:

  1. > db.help();
  2. DB methods:
  3.         db.addUser(username, password) 添加数据库授权用户
  4.         db.auth(username, password)                访问 认证
  5.         db.cloneDatabase(fromhost) 克隆数据库
  6.         db.commandHelp(name) returns the help for the command
  7.         db.copyDatabase(fromdb, todb, fromhost)  复制数据库
  8.         db.createCollection(name, { size : ..., capped : ..., max : ... } ) 创建表
  9.         db.currentOp() displays the current operation in the db
  10.         db.dropDatabase()        删除当前数据库
  11.         db.eval(func, args) run code server-side
  12.         db.getCollection(cname) same as db['cname'] or db.cname
  13.         db.getCollectionNames()        获取当前数据库的表名
  14.         db.getLastError() - just returns the err msg string
  15.         db.getLastErrorObj() - return full status object
  16.         db.getMongo() get the server connection object
  17.         db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
  18.         db.getName()
  19.         db.getPrevError()
  20.         db.getProfilingLevel()
  21.         db.getReplicationInfo()
  22.         db.getSisterDB(name) get the db at the same server as this onew
  23.         db.killOp() kills the current operation in the db
  24.         db.printCollectionStats()   打印各表的状态信息
  25.         db.printReplicationInfo()        打印主数据库的复制状态信息
  26.         db.printSlaveReplicationInfo()        打印从数据库的复制状态信息
  27.         db.printShardingStatus()                打印分片状态信息
  28.         db.removeUser(username) 删除数据库用户
  29.         db.repairDatabase() 修复数据库
  30.         db.resetError()
  31.         db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
  32.         db.setProfilingLevel(level) 0=off 1=slow 2=all
  33.         db.shutdownServer ()
  34.         db.version() current version of the server

如果想知道当前数据库下的表或者表 collection支持哪些方法,可以使用一下命令如:

  1. > db.user.help();  user为表名
  2. DBCollection help
  3.         db.foo.count()                统计表的行数
  4.         db.foo.dataSize()        统计表数据的大小
  5.         db.foo.distinct( key ) - eg. db.foo.distinct( 'x' )                按照给定的条件除重
  6.         db.foo.drop() drop the collection 删除表
  7.         db.foo.dropIndex(name)  删除指定索引
  8.         db.foo.dropIndexes() 删除所有索引
  9.         db.foo.ensureIndex(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups  增加索引
  10.         db.foo.find( [query] , [fields]) - first parameter is an optional query filter. second parameter is optional set of fields to return. 根据条件查找数据
  11.                                            e.g. db.foo.find( { x : 77 } , { name : 1 , x : 1 } )
  12.         db.foo.find(...).count()
  13.         db.foo.find(...).limit(n) 根据条件查找数据并返回指定记录数
  14.         db.foo.find(...).skip(n)
  15.         db.foo.find(...).sort(...) 查找排序
  16.         db.foo.findOne([query]) 根据条件查询只查询一条数据
  17.         db.foo.getDB() get DB object associated with collection  返回表所属的库
  18.         db.foo.getIndexes() 显示表的所有索引
  19.         db.foo.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } ) 根据条件分组
  20.         db.foo.mapReduce( mapFunction , reduceFunction , <optional params> )
  21.         db.foo.remove(query) 根据条件删除数据
  22.         db.foo.renameCollection( newName ) renames the collection  重命名表
  23.         db.foo.save(obj) 保存数据
  24.         db.foo.stats()  查看表的状态
  25.         db.foo.storageSize() - includes free space allocated to this collection 查询分配到表空间大小
  26.         db.foo.totalIndexSize() - size in bytes of all the indexes 查询所有索引的大小
  27.         db.foo.totalSize() - storage allocated for all data and indexes 查询表的总大小
  28.         db.foo.update(query, object[, upsert_bool]) 根据条件更新数据
  29.         db.foo.validate() - SLOW 验证表的详细信息
  30.         db.foo.getShardVersion() - only for use with sharding

Mongodb的备份工具 mongodump

如果想备份数据库test 如:

  1. [falcon@www.fwphp .cn  ~/mongodb/bin]$ ./mongodump --help
  2. options:
  3.   --help                   produce help message
  4.   -h [ --host ] arg        mongo host to connect to
  5.   -d [ --db ] arg          database to use
  6.   -c [ --collection ] arg  collection to use (some commands)
  7.   -u [ --username ] arg    username
  8.   -p [ --password ] arg    password
  9.   --dbpath arg             directly access mongod data files in this path,
  10.                            instead of connecting to a mongod instance
  11.   -v [ --verbose ]         be more verbose (include multiple times for more
  12.                            verbosity e.g. -vvvvv)
  13.   -o [ --out ] arg (=dump) output directory
  14. [falcon@www.fwphp.cn  ~/mongodb/bin]$ [color=Blue]./mongodump -d test -o test/[/color]
  15. connected to: 127.0.0.1
  16. DATABASE: test         to         test/test
  17.         test.user to test/test/user.bson
  18.                  100000 objects
  19.         test.system.indexes to test/test/system.indexes.bson
  20.                  1 objects
  21. [falcon@www.fwphp.cn  ~/mongodb/bin]$ ls
  22. 2     mongo   mongodump    mongofiles   mongorestore  mongosniff
  23. dump  mongod  mongoexport  mongoimport  mongos     test
linux
相关资讯       MongoDB 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款