MongoDB的使用技巧(转)
生活随笔
收集整理的這篇文章主要介紹了
MongoDB的使用技巧(转)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近常用到mongo. 看朋友這篇命令挺全的就轉(zhuǎn)來了。
如果想查看當(dāng)前連接在哪個(gè)數(shù)據(jù)庫 下面,可以直接輸入db > db
Admin
想切換到test數(shù)據(jù)庫下面 > use test
switched to db test
> db
Test
想查看test下有哪些表或者叫collection,可以輸入 > show collections
system.indexes
user
想知道m(xù)ongodb支持哪些命令,可以直接輸入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
如果想知道當(dāng)前數(shù)據(jù)庫支持哪些方法: > db.help();
DB methods:
db.addUser(username, password) 添加數(shù)據(jù)庫授權(quán)用戶
db.auth(username, password) 訪問認(rèn)證
db.cloneDatabase(fromhost) 克隆數(shù)據(jù)庫
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost) 復(fù)制數(shù)據(jù)庫
db.createCollection(name, { size : ..., capped : ..., max : ... } ) 創(chuàng)建表
db.currentOp() displays the current operation in the db
db.dropDatabase() 刪除當(dāng)前數(shù)據(jù)庫
db.eval(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames() 獲取當(dāng)前數(shù)據(jù)庫的表名
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() 打印各表的狀態(tài)信息
db.printReplicationInfo() 打印主數(shù)據(jù)庫的復(fù)制狀態(tài)信息
db.printSlaveReplicationInfo() 打印從數(shù)據(jù)庫的復(fù)制狀態(tài)信息
db.printShardingStatus() 打印分片狀態(tài)信息
db.removeUser(username) 刪除數(shù)據(jù)庫用戶
db.repairDatabase() 修復(fù)數(shù)據(jù)庫
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
如果想知道當(dāng)前數(shù)據(jù)庫下的表或者表collection支持哪些方法,可以使用一下命令如: > db.user.help(); user為表名
DBCollection help
db.foo.count() 統(tǒng)計(jì)表的行數(shù)
db.foo.dataSize() 統(tǒng)計(jì)表數(shù)據(jù)的大小
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. 根據(jù)條件查找數(shù)據(jù)
e.g. db.foo.find( { x : } , { name : , x : } )
db.foo.find(...).count()
db.foo.find(...).limit(n) 根據(jù)條件查找數(shù)據(jù)并返回指定記錄數(shù)
db.foo.find(...).skip(n)
db.foo.find(...).sort(...) 查找排序
db.foo.findOne([query]) 根據(jù)條件查詢只查詢一條數(shù)據(jù)
db.foo.getDB() get DB object associated with collection 返回表所屬的庫
db.foo.getIndexes() 顯示表的所有索引
db.foo.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } ) 根據(jù)條件分組
db.foo.mapReduce( mapFunction , reduceFunction , <optional params> )
db.foo.remove(query) 根據(jù)條件刪除數(shù)據(jù)
db.foo.renameCollection( newName ) renames the collection 重命名表
db.foo.save(obj) 保存數(shù)據(jù)
db.foo.stats() 查看表的狀態(tài)
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]) 根據(jù)條件更新數(shù)據(jù)
db.foo.validate() - SLOW 驗(yàn)證表的詳細(xì)信息
db.foo.getShardVersion() - only for use with sharding
總結(jié)
以上是生活随笔為你收集整理的MongoDB的使用技巧(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初识RabbitMQ
- 下一篇: 四级技能修炼NPC脚本参考