用户登录
用户注册

分享至

MongoDB shell 操作

  • 作者: 医穴淋床叫兽
  • 来源: 51数据库
  • 2020-08-08
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 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                                will not run the ".mongorc.js" file on 
                                        start 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 by 
                                        default) 
 
file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified 

B:命令

E:\MongoDB\mongodb_64\bin>mongo 127.0.0.1 
MongoDB shell version: 2.4.6 
connecting to: 127.0.0.1/test 
> help 
        db.help()                    help on db methods 
        db.mycoll.help()             help on collection methods 
        sh.help()                    sharding helpers 
        rs.help()                    replica set helpers 
        help admin                   administrative help 
        help connect                 connecting to a db help 
        help keys                    key shortcuts 
        help misc                    misc things to know 
        help mr                      mapreduce 
 
        show dbs                     显示数据库 
        show collections             显示当前库的表 
        show users                   显示当前库的用户 
        show profile                 show most recent system.profile entries with time >= 1ms 
        show logs                    show the accessible logger names 
        show log [name]              prints out the last segment of log in memory, 'global' is default 
        use <db_name>                切换数据库 
        db.foo.find()                显示表foo的所有记录 
        db.foo.find( { a : 1 } )     在表foo用条件查询 where a == 1 
        it                           result of the last line evaluated; use to further iterate 
        DBQuery.shellBatchSize = x   set default number of items to display on shell 
        exit                         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/test 
2. 显示数据库

setA:PRIMARY> show dbs 
admin   (empty) 
kmfile  0.078125GB 
local   10.0732421875GB 
test    15.9462890625GB 

3. 显示表

setA:PRIMARY> show collections 
KM_FILE 
file_info 
fs.chunks 
fs.files 
people 
system.indexes 
4.显示用户

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> 


setA:PRIMARY> db.file_info.find({fileName:/a.*/i}) 
{ "_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" : "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") } 
{ "_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" : "54895ecd091fb78c417fe0f4", "fileName" : "Fedora-Live-Desktop-x86_64-20-1.iso", "uploadTime" : ISODate("2014-12-11T09:10 
:21.648Z") } 
{ "_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") } 

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



软件
前端设计
程序设计
Java相关