最新內(nèi)容會在源站更新.轉(zhuǎn)載請保留原文鏈接: http://dashidan.com/article/MongoDB/index.html
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、錦江網(wǎng)絡(luò)推廣、微信小程序開發(fā)、錦江網(wǎng)絡(luò)營銷、錦江企業(yè)策劃、錦江品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供錦江建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
① mongo命令行參數(shù)幫助
通過--help
參數(shù)顯示命令行幫助信息
mongo --help
顯示:
MongoDB shell version: 2.0.4usage: 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 9999options: --shell run the shell after executing files --nodb don't connect to mongod on startup - no 'db address' arg expected --norc 開始不執(zhí)行".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支持(默認關(guān)閉)
② mongo指令幫助
通過命令提示符連上mongo數(shù)據(jù)庫后,可以輸入help
來顯示命令提示符幫助.
help
會顯示:
db.help() 數(shù)據(jù)庫方法幫助信息db.mycoll.help() 集合方法幫助信息rs.help() help on replica set methods help admin 管理員幫助信息help connect 連接數(shù)據(jù)庫幫助help keys 快捷鍵help misc 雜項信息幫助help mr mapreduce幫助show dbs 顯示全部數(shù)據(jù)庫名show collections 顯示當前數(shù)據(jù)庫中的全部集合名show users 顯示當前數(shù)據(jù)庫的全部用戶show profile show most recent system.profile entries with time >= 1msshow logs 顯示可以連接的日志(`logger`)名show log [name] 輸出內(nèi)存中的最近log的片段, 默認輸出`global`use設(shè)定當前數(shù)據(jù)庫db.foo.find() 顯示集合`foo`中的對象列表db.foo.find( { a : 1 } ) 查詢foo集合中`a == 1`的對象it 輸入it, 繼續(xù)迭代顯示結(jié)果, 輸出更多結(jié)果DBQuery.shellBatchSize = x 設(shè)置顯示結(jié)果條數(shù)exit 退出命令行
③ 數(shù)據(jù)庫幫助
show dbs
db.help()
顯示一個數(shù)據(jù)的方法的具體實現(xiàn),輸入db.
不帶()
. 例如:
db.updateUser
顯示:
test.updateUser
④ 集合幫助
show collections
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 ,)db.collection.remove(query)db.collection.renameCollection( newName , ) renames the collection.db.collection.runCommand( name , ) 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( ) - SLOW db.collection.getShardVersion() - only for use with sharding db.collection.getShardDistribution() - prints statistics about data distribution in the cluster
顯示方法實現(xiàn)輸入db.
, 不帶()
. 例如輸入:
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()
行為和結(jié)果.
顯示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 objectCursor methods .forEach( func ) .map( func ) .hasNext() .next()
看cursor
方法的實現(xiàn), 輸入db.
, 不包含()
. 例如:
db.collection.find().toArray
顯示:
> db.collection.find().toArrayfunction () { if (this._arr) { return this._arr; } var a = []; while (this.hasNext()) { a.push(this.next()); } this._arr = a; return a;}
常用的cursor
方法
hasNext() 查詢cursor
是否還有數(shù)據(jù).
next() 返回下一個數(shù)據(jù)對象, 并且cursor
指向位置加1.
forEach(
) 方法遍歷執(zhí)行全部結(jié)果. 只有1參數(shù), 迭代器中指向的對象.
⑥ 包裝對象幫助
可以通過輸入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)
⑦ 參考文章
官方文檔