一、基础知识
Nosql,是什么?不了解的自行查询,这篇我们来说说Nosql中的很常用的MongoDB
【安装】
a、下载压缩包,解压到一个路径【D:\MongoDB】
b、新建一个MongoDB数据库地址【D:\MongoData】
c、cmd命令转到MonogDB的bin路径下面,执行【mongod.exe --dbpath D:\MongoData】
d、配置环境变量path变量里面添加mongoDB的bin路径【D:\MongoDB\mongodb-win32-x86_64-2008plus-2.6.6\bin】
【对比】
二、常用操作
为方便操作,可以将MongoDB的路径配置到环境变量中,然后写成一个bat文件行进操作
新建bat文件
Start.bat
mongod --dbpath D:\MongoData
Client.bat
mongo 127.0.0.1:27017/admin
增删改查:
【增】
<span style="font-family:KaiTi_GB2312;font-size:18px;">单个插入: db.persons.insert({name:"ming"}) 批量插入: for循环插入 > db.person.drop(); true > for(var i=0;i<5;i++){ ... db.person.insert({_id:i,name:"user"+i}) ... } WriteResult({ "nInserted" : 1 }) > db.person.find(); { "_id" : 0,"name" : "user0" } { "_id" : 1,"name" : "user1" } { "_id" : 2,"name" : "user2" } { "_id" : 3,"name" : "user3" } { "_id" : 4,"name" : "user4" } > </span>
【更】
<span style="font-family:KaiTi_GB2312;">1、根据名称更新 db.persons.update({name:"nana"},{$set:{name:"meimei"}}) 2、先查询再更新 > var p=db.persons.findOne(); > p { "_id" : ObjectId("559bd4678836d241891c55c3"),"name" : "leilei" } > db.persons.update(p,{name:"admin"}) WriteResult({ "nMatched" : 1,"nUpserted" : 0,"nModified" : 1 }) > db.persons.findOne(); { "_id" : ObjectId("559bd4678836d241891c55c3"),"name" : "admin" } ></span>
【存】
<span style="font-family:KaiTi_GB2312;">1、db.persons.save({_id:1,name:"leilei"}) 2、db.persons.save({_id:1,name:"leilei2"}) 第一句执行的时候,数据库中没有该数据,所以是插入 第二句执行的时候,数据库中有该数据,所以是更新,判断数据库中有没有是根据Id来判断的</span>
【删】
<span style="font-family:KaiTi_GB2312;">a、删除记录 <span style="white-space:pre"> </span>1、根据条件删除 <span style="white-space:pre"> </span>> db.persons.remove({name:"meimei"}) <span style="white-space:pre"> </span>WriteResult({ "nRemoved" : 1 }) <span style="white-space:pre"> </span>> db.persons.find(); <span style="white-space:pre"> </span>{ "_id" : ObjectId("559bd4678836d241891c55c3"),"name" : "admin" } <span style="white-space:pre"> </span>{ "_id" : ObjectId("559bd7b00a0151afa92dec98"),"name" : "ming" } <span style="white-space:pre"> </span>> b、删除表 <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>db.persons.drop() <span style="white-space:pre"> </span> c、删除数据库 <span style="white-space:pre"> </span> use foobar <span style="white-space:pre"> </span> db.dropDatabase() "dropped" : "foobar","ok" : 1 }</span>
【查】
<span style="font-family:KaiTi_GB2312;">1、根据条件查询 var p1=db.persons.find({name:"meimei"}) p1 "_id" : ObjectId("559bd6c90cff3374e8e3cf54"),"name" : "meimei" } 2、指定返回值 find({},{key:0,key1:1}),为0表示不显示,为1表示显示 > db.groups.find({_id:1,name:1}) > db.groups.find({},{_id:1,name:1}) { "_id" : 1,"name" : "项目经理" } { "_id" : 2,"name" : "程序员" } { "_id" : 3,"name" : "测试" } { "_id" : "sd","name" : "test" } > db.groups.find({},{_id:,name:1}) 2015-07-08T09:47:53.400+0800 SyntaxError: Unexpected token,> db.groups.find({},{_id:0,name:1}) { "name" : "项目经理" } { "name" : "程序员" } { "name" : "测试" } { "name" : "test" } ></span>
三、特别的
【固定集合】
顾名思义,长度或者容量是固定的,最常用的地方是日志记录!
<span style="font-family:KaiTi_GB2312;">创建固定集合: db.createCollection("mycoll",{size:100,capped:true,max:10}) 插入数据: for(var i=0;i<10;i++){ db.mycoll.save({_id:i,name:"user"+i}) } 结果: { "_id" : 0,"name" : "user0" } { "_id" : 1,"name" : "user1" } { "_id" : 2,"name" : "user2" } { "_id" : 3,"name" : "user3" } { "_id" : 4,"name" : "user4" } { "_id" : 5,"name" : "user5" } { "_id" : 6,"name" : "user6" } { "_id" : 7,"name" : "user7" } { "_id" : 8,"name" : "user8" } { "_id" : 9,"name" : "user9" } 新插入一条记录: db.mycoll.save({_id:10,name:"user10"}) 结果 { "_id" : 1,"name" : "user9" } { "_id" : 10,"name" : "user10" } 固定集合总是把最初插入的数据给覆盖掉</span>
【文件存储】
GridFS是MongoDB自带文件系统,使用二进制的形式进行存储,利用工具mongofiles.exe。
<span style="font-family:KaiTi_GB2312;font-size:14px;">C:\Users\leilei>mongofiles -d files -l "d:\test\mongo\a.txt" put "a.txt" <span style="white-space: pre;"> </span>connected to: 127.0.0.1 <span style="white-space: pre;"> </span>added file: { _id: ObjectId('559ccce1656df04fea47a43a'),filename: "a.txt",chun <span style="white-space: pre;"> </span>kSize: 261120,uploadDate: new Date(1436339427517),md5: "bd1ef306bb4ee8d699ad74 <span style="white-space: pre;"> </span>f081be069e",length: 52 } <span style="white-space: pre;"> </span>done! 参数解释: <span style="white-space: pre;"> </span>-d:数据库名称 <span style="white-space: pre;"> </span>-l:文件所在本地的位置 <span style="white-space: pre;"> </span>put:将文件存入到数据库中 <span style="white-space: pre;"> </span>存取的文件名称</span><span style="font-size:18px;"> </span>
2、查看存储的文件信息
<span style="font-family:KaiTi_GB2312;font-size:14px;">db.fs.files.find() { "_id" : ObjectId("559ccce1656df04fea47a43a"),"filename" : "a.txt","chunkSize" : 261120,"uploadDate" : ISODate("2015-07-08T07:10:27.517Z"),"md5" : "bd1ef306bb4ee8d699ad74f081be069e","length" : 52 } { "_id" : ObjectId("559cce547cbd190997f72238"),"filename" : "b.docx","uploadDate" : ISODate("2015-07-08T07:16:36.712Z"),"md5" : "71653152f11c70599aa388671e9f29ce","length" : 16296 } mongofiles -d files list connected to: 127.0.0.1 a.txt 52 b.docx 16296</span><span style="font-size:18px;"> </span>
mongofiles -d files delete "b.docx" <span style="white-space:pre"> </span>connected to: 127.0.0.1 <span style="white-space:pre"> </span>done!
(未完,下篇主从复制)
原文链接:https://www.f2er.com/nosql/203849.html