MongoDB的使用技巧(转)
生活随笔
收集整理的這篇文章主要介紹了
MongoDB的使用技巧(转)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近常用到mongo. 看朋友這篇命令挺全的就轉來了。
如果想查看當前連接在哪個數據庫 下面,可以直接輸入db > db
Admin
想切換到test數據庫下面 > use test
switched to db test
> db
Test
想查看test下有哪些表或者叫collection,可以輸入 > show collections
system.indexes
user
想知道mongodb支持哪些命令,可以直接輸入help > help
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 curent database to <db name>
db.help() help on DB methods
db.foo.help() help on collection methods
db.foo.find() list objects in collection foo
db.foo.find( { a : } ) list objects in foo where a ==
it result of the last line evaluated; use to further iterate
如果想知道當前數據庫支持哪些方法: > db.help();
DB methods:
db.addUser(username, password) 添加數據庫授權用戶
db.auth(username, password) 訪問認證
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()
db.getReplicationInfo()
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() kills the current operation in the db
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 : }
db.setProfilingLevel(level) =off =slow =all
db.shutdownServer()
db.version() current version of the server
如果想知道當前數據庫下的表或者表collection支持哪些方法,可以使用一下命令如: > db.user.help(); user為表名
DBCollection help
db.foo.count() 統計表的行數
db.foo.dataSize() 統計表數據的大小
db.foo.distinct( key ) - eg. db.foo.distinct( 'x' ) 按照給定的條件除重
db.foo.drop() drop the collection 刪除表
db.foo.dropIndex(name) 刪除指定索引
db.foo.dropIndexes() 刪除所有索引
db.foo.ensureIndex(keypattern,options) - options should be an object with these possible fields: name, unique, dropDups 增加索引
db.foo.find( [query] , [fields]) - first parameter is an optional query filter. second parameter is optional set of fields to return. 根據條件查找數據
e.g. db.foo.find( { x : } , { name : , x : } )
db.foo.find(...).count()
db.foo.find(...).limit(n) 根據條件查找數據并返回指定記錄數
db.foo.find(...).skip(n)
db.foo.find(...).sort(...) 查找排序
db.foo.findOne([query]) 根據條件查詢只查詢一條數據
db.foo.getDB() get DB object associated with collection 返回表所屬的庫
db.foo.getIndexes() 顯示表的所有索引
db.foo.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } ) 根據條件分組
db.foo.mapReduce( mapFunction , reduceFunction , <optional params> )
db.foo.remove(query) 根據條件刪除數據
db.foo.renameCollection( newName ) renames the collection 重命名表
db.foo.save(obj) 保存數據
db.foo.stats() 查看表的狀態
db.foo.storageSize() - includes free space allocated to this collection 查詢分配到表空間大小
db.foo.totalIndexSize() - size in bytes of all the indexes 查詢所有索引的大小
db.foo.totalSize() - storage allocated for all data and indexes 查詢表的總大小
db.foo.update(query, object[, upsert_bool]) 根據條件更新數據
db.foo.validate() - SLOW 驗證表的詳細信息
db.foo.getShardVersion() - only for use with sharding
總結
以上是生活随笔為你收集整理的MongoDB的使用技巧(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初识RabbitMQ
- 下一篇: 四级技能修炼NPC脚本参考