6.mongo命令提示符幫助
最新內容會在源站更新。
轉載請保留原文鏈接: http://dashidan.com/article/mongodb/basic/6.html
① mongo命令行參數幫助
通過--help
參數顯示命令行幫助信息
mongo --help
顯示:
MongoDB shell version: 2.0.4
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
--norc 開始不執行".mongorc.js"文件
--quiet 安靜模式
--port arg 端口
--host arg IP
--eval arg 運行javascript腳本
-u [ --username ] arg 用戶名
-p [ --password ] arg 密碼
-h [ --help ] 顯示這個幫助信息
--version 版本號
--verbose increase verbosity
--ipv6 開啓IPv6支持(默認關閉)
② mongo指令幫助
通過命令提示符連上mongo數據庫後,可以輸入help
來顯示命令提示符幫助.
help
會顯示:
db.help() 數據庫方法幫助信息
db.mycoll.help() 集合方法幫助信息
rs.help() help on replica set methods
help admin 管理員幫助信息
help connect 連接數據庫幫助
help keys 快捷鍵
help misc 雜項信息幫助
help mr mapreduce幫助
show dbs 顯示全部數據庫名
show collections 顯示當前數據庫中的全部集合名
show users 顯示當前數據庫的全部用戶
show profile show most recent system.profile entries with time >= 1ms
show logs 顯示可以連接的日誌(`logger`)名
show log [name] 輸出內存中的最近log的片段, 默認輸出`global`
use <db_name> 設定當前數據庫
db.foo.find() 顯示集合`foo`中的對象列表
db.foo.find( { a : 1 } ) 查詢foo集合中`a == 1`的對象
it 輸入it, 繼續迭代顯示結果, 輸出更多結果
DBQuery.shellBatchSize = x 設置顯示結果條數
exit 退出命令行
③ 數據庫幫助
1.顯示全部數據庫
show dbs
2.顯示部數據操作幫助
db.help()
3.顯示方法的實現
顯示一個數據的方法的具體實現,輸入db.<method name>
不帶()
.
例如:
db.updateUser
顯示:
test.updateUser
④ 集合幫助
1.顯示全部集合
show collections
2.顯示集合幫助
db.collection.help()
顯示
db.collection.find().help() - show DBCursor help
db.collection.count()
db.collection.dataSize()
db.collection.distinct( key ) - eg. db.collection.distinct( 'x' )
db.collection.drop() drop the collection
db.collection.dropIndex(name)
db.collection.dropIndexes()
db.collection.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
db.collection.reIndex()
db.collection.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
e.g. db.collection.find( {x:77} , {name:1, x:1} )
db.collection.find(...).count()
db.collection.find(...).limit(n)
db.collection.find(...).skip(n)
db.collection.find(...).sort(...)
db.collection.findOne([query])
db.collection.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
db.collection.getDB() get DB object associated with collection
db.collection.getIndexes()
db.collection.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
db.collection.mapReduce( mapFunction , reduceFunction , <optional params> )
db.collection.remove(query)
db.collection.renameCollection( newName , <dropTarget> ) renames the collection.
db.collection.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
db.collection.save(obj)
db.collection.stats()
db.collection.storageSize() - includes free space allocated to this collection
db.collection.totalIndexSize() - size in bytes of all the indexes
db.collection.totalSize() - storage allocated for all data and indexes
db.collection.update(query, object[, upsert_bool, multi_bool])
db.collection.validate( <full> ) - SLOW
db.collection.getShardVersion() - only for use with sharding
db.collection.getShardDistribution() - prints statistics about data distribution in the cluster
3.顯示集合方法實現
顯示方法實現輸入db.<collection>.<method>
, 不帶()
.
例如輸入:
db.collection.save
顯示:
function (obj) {
if (obj == null || typeof obj == "undefined") {
throw "can't save a null";
}
if (typeof obj == "number" || typeof obj == "string") {
throw "can't save a number or string";
}
if (typeof obj._id == "undefined") {
obj._id = new ObjectId;
return this.insert(obj);
} else {
return this.update({_id:obj._id}, obj, true);
}
}
⑤ cursor幫助
當你在mongo命令行使用find()
方法時, 可以使用很多cursor
方法來修改find()
行爲和結果.
顯示find()方法可用的修改器和
cursor
處理方法db.collection.find().help()
顯示:
> db.collection.find().help()
find() modifiers
.sort( {...} )
.limit( n )
.skip( n )
.count() - total # of objects matching query, ignores skip,limit
.size() - total # of objects cursor would return, honors skip,limit
.explain([verbose])
.hint(...)
.showDiskLoc() - adds a $diskLoc field to each returned object
Cursor methods
.forEach( func )
.map( func )
.hasNext()
.next()
- 看
cursor
方法的實現, 輸入db.<collection>.find().<method>
, 不包含()
.
例如:
db.collection.find().toArray
顯示:
> db.collection.find().toArray
function () {
if (this._arr) {
return this._arr;
}
var a = [];
while (this.hasNext()) {
a.push(this.next());
}
this._arr = a;
return a;
}
常用的cursor
方法
- hasNext() 查詢
cursor
是否還有數據. - next() 返回下一個數據對象, 並且
cursor
指向位置加1. - forEach() 方法遍歷執行全部結果.只有1參數, 迭代器中指向的對象.
⑥ 包裝對象幫助
可以通過輸入help misc
來獲取對象包裝類.
help misc
顯示:
> help misc
b = new BinData(subtype,base64str) create a BSON BinData value
b.subtype() the BinData subtype (0..255)
b.length() length of the BinData data in bytes
b.hex() the data as a hex encoded string
b.base64() the data as a base 64 encoded string
b.toString()
b = HexData(subtype,hexstr) create a BSON BinData value from a hex string
b = UUID(hexstr) create a BSON BinData value of UUID subtype
b = MD5(hexstr) create a BSON BinData value of MD5 subtype
o = new ObjectId() create a new ObjectId
o.getTimestamp() return timestamp derived from first 32 bits of the OID
o.isObjectId()
o.toString()
o.equals(otherid)