MongoDB shell 操作
生活随笔
收集整理的這篇文章主要介紹了
MongoDB shell 操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
A:連接命令
E:\MongoDB\mongodb_64\bin>mongo --help MongoDB shell version: 2.4.6 usage: mongo [options] [db address] [file names (ending in .js)] db address can be:foo foo database on local machine192.169.0.5/foo foo database on 192.168.0.5 machine192.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 will not run the ".mongorc.js" file onstart up--quiet be less chatty--port arg port to connect to--host arg server to connect to--eval arg evaluate javascript-u [ --username ] arg username for authentication-p [ --password ] arg password for authentication--authenticationDatabase arg user source (defaults to dbname)--authenticationMechanism arg (=MONGODB-CR)authentication mechanism-h [ --help ] show this usage information--version show version information--verbose increase verbosity--ipv6 enable IPv6 support (disabled bydefault)file names: a list of files to run. files have to end in .js and will exit after unless --shell is specifiedB:命令 E:\MongoDB\mongodb_64\bin>mongo 127.0.0.1 MongoDB shell version: 2.4.6 connecting to: 127.0.0.1/test > helpdb.help() help on db methodsdb.mycoll.help() help on collection methodssh.help() sharding helpersrs.help() replica set helpershelp admin administrative helphelp connect connecting to a db helphelp keys key shortcutshelp misc misc things to knowhelp mr mapreduceshow dbs 顯示數據庫show collections 顯示當前庫的表show users 顯示當前庫的用戶show profile show most recent system.profile entries with time >= 1msshow logs show the accessible logger namesshow log [name] prints out the last segment of log in memory, 'global' is defaultuse <db_name> 切換數據庫db.foo.find() 顯示表foo的所有記錄db.foo.find( { a : 1 } ) 在表foo用條件查詢 where a == 1it result of the last line evaluated; use to further iterateDBQuery.shellBatchSize = x set default number of items to display on shellexit quit the mongo shell >
C:操作
1. 連接
E:\MongoDB\mongodb_64\bin>mongo 192.168.0.99 MongoDB shell version: 2.4.6 connecting to: 192.168.0.99/test2. 顯示數據庫 setA:PRIMARY> show dbs admin (empty) kmfile 0.078125GB local 10.0732421875GB test 15.9462890625GB3. 顯示表 setA:PRIMARY> show collections KM_FILE file_info fs.chunks fs.files people system.indexes4.顯示用戶
setA:PRIMARY> show users {"_id" : ObjectId("54953b4358992d19b0cdf751"),"user" : "root","readOnly" : false,"pwd" : "8c81aae2cd9cb582540cbe5cbb376dfe" }
5.顯示表的記錄
setA:PRIMARY> db.KM_FILE.find() { "_id" : ObjectId("545994e56fc4e95b2877a751"), "name" : "test" }
setA:PRIMARY> db.file_info.find() { "_id" : ObjectId("54893fcb33ab4f8c34d3fbcb"), "fileName" : "Xiaoguxian_OKCoin_Setup3.1.exe", "id" : "54893fcb33ab4f8c34d3fbc5", "uploadTime" : ISODate("2014-12-11T06:55:07.717Z") } { "_id" : ObjectId("548949328aea54e8436d3ed9"), "fileName" : "QQPlayer_Setup_37_892.exe", "id" : "5489492f8aea54e8436d3e6e", "uplo adTime" : ISODate("2014-12-11T07:35:14.049Z") } { "_id" : "54895140e95878503b38c138", "fileName" : "jAVA基礎+高新+7K知識點總結.chm", "uploadTime" : ISODate("2014-12-11T08:09:36.2 35Z") } { "_id" : "5489572c86e83f10416107a8", "fileName" : "360bdoctor.exe", "uploadTime" : ISODate("2014-12-11T08:34:52.187Z") } { "_id" : "5489575786e83f10416107b1", "fileName" : "Axure RP.zip", "uploadTime" : ISODate("2014-12-11T08:35:41.452Z") } { "_id" : "5489576586e83f104161089f", "fileName" : "Info.plist", "uploadTime" : ISODate("2014-12-11T08:35:49.528Z") } { "_id" : "5489576986e83f10416108a1", "fileName" : "Info.plist", "uploadTime" : ISODate("2014-12-11T08:35:53.653Z") } { "_id" : "548957a2cc7c566c2d8072b5", "fileName" : "Axure RP.zip", "uploadTime" : ISODate("2014-12-11T08:36:56.631Z") } { "_id" : "548957e2cc7c566c2d8073a4", "fileName" : "MongoDB權威指南中文版.pdf .pdf", "uploadTime" : ISODate("2014-12-11T08:37:55.6 07Z") } { "_id" : "54895819cc7c566c2d807427", "fileName" : "bootstrap-date.html", "uploadTime" : ISODate("2014-12-11T08:38:49.603Z") } { "_id" : "54895e3c091fb78c417fca3c", "fileName" : "dataset_617740.rar", "uploadTime" : ISODate("2014-12-11T09:07:13.232Z") } { "_id" : "54895ef4091fb78c417fe7c2", "fileName" : "iTunes64Setup.exe", "uploadTime" : ISODate("2014-12-11T09:08:34.981Z") } { "_id" : "54896423ba85e7903d97d1f7", "fileName" : "all-orgs-strings.lc.txt", "uploadTime" : ISODate("2014-12-11T09:30:11.816Z") } { "_id" : "548966b1ba85e7903d97d1fa", "fileName" : "aaa-201411140001.pdf", "uploadTime" : ISODate("2014-12-11T09:41:06.093Z") }
6.查詢 setA:PRIMARY> db.file_info.find({fileName:"aaa-201411140001.pdf"}) { "_id" : "548966b1ba85e7903d97d1fa", "fileName" : "aaa-201411140001.pdf", "uplo adTime" : ISODate("2014-12-11T09:41:06.093Z") }
7.模糊查詢 setA:PRIMARY> db.file_info.find({fileName:/Ax.*/i}) { "_id" : "5489575786e83f10416107b1", "fileName" : "Axure RP.zip", "uploadTime" : ISODate("2014-12-11T08:35:41.452Z") } { "_id" : "548957a2cc7c566c2d8072b5", "fileName" : "Axure RP.zip", "uploadTime" : ISODate("2014-12-11T08:36:56.631Z") } setA:PRIMARY> db.file_info.find({fileName:/aa.*/i}) { "_id" : "548966b1ba85e7903d97d1fa", "fileName" : "aaa-201411140001.pdf", "uploadTime" : ISODate("2014-12-11T09:41:06.093Z") } setA:PRIMARY>
8:group by?
> db.users.group({key:{sex:true,vip:true},cond:{age:20},reduce: function(obj,prev){prev.ccost+=1},initial:{ccost:0}}); [{"sex" : "男","vip" : "普通用戶","ccost" : 5531},{"sex" : "女","vip" : "普通用戶","ccost" : 6108},{"sex" : "男","vip" : "銀卡","ccost" : 3},{"sex" : "女","vip" : "金卡","ccost" : 1} ] >導入CVS
mongoimport -d"db5" -c"users" -f"id,name,phone,diqu,age,sex,vip,cdate,cost,cardid,addr" --type=csv --file=aa.txt
總結
以上是生活随笔為你收集整理的MongoDB shell 操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于openfire+smack的And
- 下一篇: SQL解析器的性能测试