MongoDB的增删改的操作
1、 进入mongodb 与安全退出mongodb
a) 进入mongodb:进入mongodb的bin目录运行./mongo ip:port。如果使用mongdb默认端口,则可以不输入IP和端口,例子如下
默认端口(27017):
[root@sentos202 bin]#./mongo
自定义端口:
[root@sentos202 bin]# ./mongo 192.168.2.202:9999
b) 退出mongoDB
有两种退出方式。一种暴利退出。另一种安全退出
暴利退出:找到mongoDb的进程。杀死进程号
[root@sentos202 bin]# ps -ef|grep mongod
root 13202 1 115:36 ? 00:00:01 ./mongod -fmdb.conf
root 13420 5402 015:39 pts/1 00:00:00 grep mongod
[root@sentos202 bin]# kill -9 13202
安全退出:进入mongodb 进入admin库 用命令db.shutdownServer()
[root@sentos202 bin]# ./mongo 192.168.2.202:9999
>use admin
switchedto db admin
> db.shutdownServer();
这样操作会报错。因为mongoDB关闭是要求在本服务器上关闭。不能远程连接关闭,所以连接的时候不能加上ip,一但加ip就成了客户端登录了 退出后。将连接方式改为如下:
[root@sentos202 bin]# ./mongo --port 9999
2、 建数据库与建表
a) 建库:mongoDb没有指定的建库方式。
直接使用 use 库名 可以自动创建库,当在库中保存了 对应的表(collection)后,库会自动创建
> show dbs;
local 0.000GB
> use user
switched to db user
b) 建表:mongoDb 同样没有指定的建表方式,当选择库后,执行了插入数据操作,则表会自动生成
> db.user.insert({"id":1,"name":"test1"})
WriteResult({ "nInserted" : 1 })
c) 查询所拥有的库与表
查询库:showdbs; 或者showdatabases;
> show dbs
local 0.000GB
user 0.000GB
查询表: show tables或者showcollections
> show tables;
us
或者:
> showcollections
us
d) 查询当前所用的数据库 用db
> db
user
e) 删除摸个库(删除时必须进入该库后删除,如,我删除user库)
> use user
switchedto db user
>db.dropDatabase();
{ "dropped" :"user", "ok" : 1 }
f) 删除摸个表(collections),语法:db.表名称.drop();
> use tb
switchedto db tb
> showcollections
pro
> db.pro.drop()
true
> showcollections
>
3、 对数据的CRUD操作(以下均在库 tb中进行)
a) 操作的数据格式
mongoDb的所有操作的数据格式都是用类似于json的数据格式(Bson),
其实和json格式大同小异
如: {"id":"1","name":"书"}
b) insert插入操作; 语法,db.表名.insert();
插入一个:
>db.product.insert({"id":"1","name":"书"});
WriteResult({ "nInserted" : 1 })
插入多个:(这里故意插入了一条重复数据)
> db.product.insert([{"id":"2","name":"铅笔"},{"id":"3","name":"毛笔"},{"id":"4","name":"钢笔"},{"id":"4","name":"钢笔"}]);
BulkWriteResult({
"writeErrors" :[ ],
"writeConcernErrors": [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
c) find查询操作;语法,db.表名.find();
查询所有:
>db.product.find();
{"_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书" }
{"_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dbb"),"id" : "4", "name" : "钢笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dbb"),"id" : "4", "name" : "钢笔" }
带条件查询:
>db.product.find({"id" : "1"});
{ "_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书" }
>
findOne()操作:
>db.product.find({ "name" : "钢笔"})
{ "_id" : ObjectId("5716d06e7d8b1762313d3dbb"),"id" : "4", "name" : "钢笔" }
{ "_id" : ObjectId("5716d2247d8b1762313d3dbc"),"id" : "4", "name" : "钢笔" }
> db.product.findOne({ "name" : "钢笔"})
{ "_id" : ObjectId("5716d06e7d8b1762313d3dbb"),"id" : "4", "name" : "钢笔" }
可以看出,如果用find匹配,可以匹配到所有满足条件的数据,如果使用findOne(),则只要检查到数据后,立刻返回该条数,不会继续监测剩余的数据;
查询一个并对其做操作:
MongoDB提供查询到一条数据后对该条数据的操作,具体有以下语法:
db.product.findOneAndReplace()
db.product.findAndModify()
db.product.findOneAndDelete()
db.product.findOneAndUpdate()
对_id的说明:
_id是mongodb的数据唯一性的控制。
{"_id" : ObjectId("5716d06e7d8b1762313d3dbb"),"id" : "4", "name" : "钢笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dbb"),"id" : "4", "name" : "钢笔" }
在插入数据的时候,mongodb会自动检查你的数据中是否有“_id”字段,如果没有mongodb自动生成;简单来说,如果你存储的数据要求自定义的id唯一性。那么你在插入之前就得生成自己自定义的唯一id
d) 查询文档(一条数据)大小 Object.bsonsize(文档名);
Mongodb要求每个文档大小不能超过16M;
> varpro6={"id":"6","name":"玩具车"}
> db.product.insert(pro6);
WriteResult({"nInserted" : 1 })
> db.product.find();
{"_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书" }
{"_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dbb"),"id" : "4", "name" : "钢笔" }
{"_id" : ObjectId("5716d2247d8b1762313d3dbc"),"id" : "4", "name" : "钢笔" }
{"_id" : ObjectId("5716d6e27d8b1762313d3dbd"),"id" : "6", "name" : "玩具车" }
>Object.bsonsize(pro6);
35
e) Remove删除操作,db.表明.remove();
带条件删除: 删除id为4的数据
> db.product.find();
{"_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书" }
{"_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dba"), "id": "3", "name" : "毛笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dbb"),"id" : "4", "name" : "钢笔" }
{"_id" : ObjectId("5716d2247d8b1762313d3dbc"),"id" : "4", "name" : "钢笔" }
{"_id" : ObjectId("5716d6e27d8b1762313d3dbd"),"id" : "6", "name" : "玩具车" }
> db.product.remove({"id":"4"});
WriteResult({"nRemoved" : 2 })
>db.product.find();
{"_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书" }
{"_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{"_id" : ObjectId("5716d6e27d8b1762313d3dbd"),"id" : "6", "name" : "玩具车" }
删除所有:Remove(),不带bson参数,则会清空表,如:>db.product.remove({});
f) Update更新操作
更新某条数据(更新时必须将不需要的字段原样赋值。否则将丢失)
> db.product.find();
{ "_id" : ObjectId("5716d0417d8b1762313d3db8"), "id": "1", "name" : "书" }
{ "_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{ "_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
>db.product.update({"id":"1"},{"id" :"1", "name" : "书","price":5.5})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
> db.product.find();
{ "_id" :ObjectId("5716d0417d8b1762313d3db8"), "id" : "1","name" : "书", "price" : 5.5 }
{ "_id" :ObjectId("5716d06e7d8b1762313d3db9"), "id" : "2","name" : "铅笔" }
{ "_id" :ObjectId("5716d06e7d8b1762313d3dba"), "id" : "3","name" : "毛笔" }
按条件匹配,如果有多条满足条件的数据只会更新第一条:
> db.product.find();
{ "_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书","price" : 5.5 }
{ "_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{ "_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{ "_id" :ObjectId("5716da8e7d8b1762313d3dbe"), "id" : "4","name" : "钢笔" }
{ "_id" :ObjectId("5716da937d8b1762313d3dbf"), "id" : "6","name" : "钢笔" }
{ "_id" :ObjectId("5716da977d8b1762313d3dc0"), "id" : "5","name" : "钢笔" }
> db.product.update({"name":"钢笔"},{"id" : "1","name" : "钢笔","price":5.5})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
> db.product.find();
{ "_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书","price" : 5.5 }
{ "_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{ "_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{"_id" : ObjectId("5716da8e7d8b1762313d3dbe"),"id" : "1", "name" : "钢笔", "price" : 5.5 }
{ "_id" :ObjectId("5716da937d8b1762313d3dbf"), "id" : "6","name" : "钢笔" }
{ "_id" :ObjectId("5716da977d8b1762313d3dc0"), "id" : "5","name" : "钢笔" }
按条件匹配,如果要批量修改满足条件的,则需要修改器来操作;
g) Update的更新修改器
$set :指定一个字段的值,如果字段不存在,会创建一个
修改匹配的第一条
> db.product.find();
{"_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书","price" : 5.5 }
{"_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{"_id" : ObjectId("5716da8e7d8b1762313d3dbe"),"Set" : { "price" : "5.5" } }
{ "_id" :ObjectId("5716da937d8b1762313d3dbf"), "id" : "6","name" : "钢笔" }
{ "_id" :ObjectId("5716da977d8b1762313d3dc0"), "id" : "5","name" : "钢笔" }
>db.product.update({"name":"钢笔"},{"$set":{"price":"5.5"}});
WriteResult({"nMatched" : 1, "nUpserted" : 0, "nModified" : 1})
>db.product.find();
{"_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书","price" : 5.5 }
{"_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{"_id" : ObjectId("5716da8e7d8b1762313d3dbe"),"Set" : { "price" : "5.5" } }
{ "_id" :ObjectId("5716da937d8b1762313d3dbf"), "id" : "6","name" : "钢笔","price" : "5.5" }
{ "_id" :ObjectId("5716da977d8b1762313d3dc0"), "id" : "5","name" : "钢笔" }
修改满足条件的所有:
>db.product.find();
{"_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书","price" : 5.5 }
{"_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{"_id" : ObjectId("5716da8e7d8b1762313d3dbe"),"Set" : { "price" : "5.5" } }
{ "_id" :ObjectId("5716da937d8b1762313d3dbf"), "id" : "6","name" : "钢笔", "price" :"5.5" }
{ "_id" :ObjectId("5716da977d8b1762313d3dc0"), "id" : "5","name" : "钢笔" }
>db.product.update({"name":"钢笔"},{$set:{"price":"2.5"}},0,1);
WriteResult({"nMatched" : 2, "nUpserted" : 0, "nModified" : 2})
>db.product.find();
{"_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书","price" : 5.5 }
{"_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{"_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{"_id" : ObjectId("5716da8e7d8b1762313d3dbe"),"Set" : { "price" : "5.5" } }
{ "_id" :ObjectId("5716da937d8b1762313d3dbf"), "id" : "6","name" : "钢笔","price" : "2.5"}
{ "_id" :ObjectId("5716da977d8b1762313d3dc0"), "id" : "5","name" : "钢笔","price" : "2.5"}
db.product.update({"name":"钢笔"},{$set:{"price":"2.5"}},0,1);分析:
总共4个参数,如果只写前两个参数,或者第四个为0,则修改匹配到的第一条。如果第四个参数为1,则修改匹配到的所有!
$unset :删掉某个字段
>db.product.find();
{ "_id" :ObjectId("5716d0417d8b1762313d3db8"), "id" : "1","name" : "书", "price": 5.5 }
{ "_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{ "_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{ "_id" : ObjectId("5716da8e7d8b1762313d3dbe"),"Set" : { "price" : "5.5" } }
{ "_id" : ObjectId("5716da937d8b1762313d3dbf"),"id" : "6", "name" : "钢笔","price" : "3.5" }
{ "_id" : ObjectId("5716da977d8b1762313d3dc0"),"id" : "5", "name" : "钢笔","price" : "3.5" }
>db.product.update({"id":"1"},{$unset:{price:1}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find();
{ "_id" : ObjectId("5716d0417d8b1762313d3db8"),"id" : "1", "name" : "书" }
{ "_id" : ObjectId("5716d06e7d8b1762313d3db9"),"id" : "2", "name" : "铅笔" }
{ "_id" : ObjectId("5716d06e7d8b1762313d3dba"),"id" : "3", "name" : "毛笔" }
{ "_id" : ObjectId("5716da8e7d8b1762313d3dbe"),"Set" : { "price" : "5.5" } }
{ "_id" : ObjectId("5716da937d8b1762313d3dbf"),"id" : "6", "name" : "钢笔","price" : "3.5" }
{ "_id" : ObjectId("5716da977d8b1762313d3dc0"),"id" : "5", "name" : "钢笔","price" : "3.5" }
语法db.product.update({"id":"1"},{$unset:{price:1}});解析:
前一个{}是匹配条件。后一个指定删除的字段,为price,1为确定删除!只能操作到匹配的第一个,如果要批量删除则用
db.product.update({"name":"钢笔"},{$unset:{price:1}},0,1);语法和批量更新类似;
如果要删除多个字段。则以,分割:如
> db.product.update({"name":"钢笔"},{$unset:{price:1,name:1}},0,1);
$inc :用来增加已有键的值,如果字段不存在,会创建一个。只能用于整型、长整型、或双精度浮点型的值。
>db.product.find();
{ "_id" :ObjectId("57182150560ca68a25e2991c"), "id" : "1","name" : "铅笔", "price": 4 }
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5 }
{ "_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
>db.product.update({"id":"1"},{$inc:{"price":5}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find();
{ "_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔", "price" : 9 }
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5 }
{ "_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
语法db.product.update({"id":"1"},{$inc:{"price":5}});分析:将id为1的商品价格加上5;减去就写负数,只能用于整型、长整型、或双精度浮点型的值。
$push:向已有数组的末尾加入一个元素,要是没有就新建一个数组
>db.product.find({"id":"2"});
{ "_id" :ObjectId("57182150560ca68a25e2991d"), "id" : "2","name" : "毛笔", "price" : 5, "scop" : [ 1, 2, 3 ] }
>db.product.update({"id":"2"},{$push:{"scop":4}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id" :ObjectId("57182150560ca68a25e2991d"), "id" : "2","name" : "毛笔", "price" : 5, "scop" : [ 1, 2, 3, 4 ] }
语法分析:db.product.update({"id":"2"},{$push:{"scop":4}}),向id为2的数据中的scop数组增加一个4;
$each:通过一次$push来操作多个值
>db.product.update({"id":"2"},{$push:{"scop":{$each:[6,7,8]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id": ObjectId("57182150560ca68a25e2991d"), "id" :"2", "name" : "毛笔","price" : 5, "scop" : [ 1, 2, 3, 4, 6, 7, 8 ] }
语法分析:db.product.update({"id":"2"},{$push:{"scop":{$each:[6,7,8]}}}),向id为2的数据中的scop数组批量增加6,7,8;$each ,$each配合使用
$slice:限制数组只包含最后加入的n个元素,其值必须是负整数
{ "_id" :ObjectId("57182150560ca68a25e2991d"), "id" : "2","name" : "毛笔", "price" : 5, "scop" : [ 1, 2, 3, 4, 6,7, 8 ] }
>db.product.update({"id":"2"},{$push:{"scop":{$each:[6,7,8],$slice:-4}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id" :ObjectId("57182150560ca68a25e2991d"), "id" : "2","name" : "毛笔", "price" : 5, "scop" : [ 8, 6, 7, 8 ] }
分析:db.product.update({"id":"2"},{$push:{"scop":{$each:[6,7,8],$slice:-4}}})
,向id为2的数据中的scop数组批量增加6,7,8;$each ,$each,和$slice配合使用,将socp的值取根据$slice指定的值取后面几个。$slice的值必须为负数
$sort:对数组中的元素,按照指定的字段来对数据进行排序(1为升序,-1为降序),然后再按照slice删除.
升序:
>db.product.find({"id":"2"});
{ "_id": ObjectId("57182150560ca68a25e2991d"), "id" :"2", "name" : "毛笔","price" : 5, "scop" : [ 8, 6, 7, 8 ] }
>db.product.update({"id":"2"},{$push:{"scop":{$each:[1,2,3],$slice:-4,$sort:1}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id": ObjectId("57182150560ca68a25e2991d"), "id" :"2", "name" : "毛笔","price" : 5, "scop" : [ 6, 7, 8, 8 ] }
降序:
>db.product.find({"id":"2"});
{ "_id": ObjectId("57182150560ca68a25e2991d"), "id" :"2", "name" : "毛笔","price" : 5, "scop" : [ 6, 7, 8, 8 ] }
>db.product.update({"id":"2"},{$push:{"scop":{$each:[1,2,3],$slice:-4,$sort:-1}}})
WriteResult({"nMatched" : 1, "nUpserted" : 0, "nModified" : 1})
>db.product.find({"id":"2"});
{ "_id": ObjectId("57182150560ca68a25e2991d"), "id" :"2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1 ] }
分析:db.product.update({"id":"2"},{$push:{"scop":{$each:[1,2,3],$slice:-4},$sort:-1}})
,向id为2的数据中的scop数组批量增加1,2,3;$each ,$each,和$slice配合使用,首先将socp的内容按照$sort指定的要求排序。再将socp的值取根据$slice指定的值取后面几个。$slice的值必须为负数
如果有字段:
db.product.update({"id":"2"},{$push:{"scop":{$each:[1,2,3],$slice:-4,$sort:{“字段”:-1}}}})
。注意:不能只将$slice或者$sort与$push配合使用,且必须使用$each
$ne:判断一个值是否在数组中,如果不在则添加进去
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1 ] }
>db.product.update({"id":"2",scop:{$ne:3}},{$push:{"scop":3}})
WriteResult({ "nMatched" : 0, "nUpserted" : 0,"nModified" : 0 })
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1 ] }
>db.product.update({"id":"2",scop:{$ne:4}},{$push:{"scop":4}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1, 4 ] }
分析:db.product.update({"id":"2",scop:{$ne: n }},{$push:{"scop": n }})
,向id为2的对象的scop添加一个数字n,如果n存在,则不添加,如果不存在则添加,
$addToSet:将数组作为数据集使用,以保证数组内的元素不会重复
>db.product.find({"id":"2"});
{ "_id": ObjectId("57182150560ca68a25e2991d"), "id" :"2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1, 4 ] }
>db.product.update({"id":"2"},{$addToSet:{"scop":4}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 0 })
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1, 4 ] }
>db.product.update({"id":"2"},{$addToSet:{"scop":5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id": ObjectId("57182150560ca68a25e2991d"), "id" :"2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1, 4, 5 ] }
分析:db.product.update({"id":"2"},{$addToSet:{"scop": n }},向id为2的对象的scop添加一个数字n,如果n存在,则不添加,如果不存在则添加,和比$ne功能相似,切更适用
$pop :从数组一端删除元素,{$pop:{key:1}},从末尾删掉一个,-1则从头部删除
db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1, 4, 5 ] }
>db.product.update({"id":"2"},{$pop:{"scop":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 3, 2, 1, 4 ] }
>db.product.update({"id":"2"},{$pop:{"scop":-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 3, 2, 1, 4 ] }
分析:db.product.update({"id":"2"},{$pop:{"scop":n}})将id为2的的socp数组从末尾删除n个数据,如果为n为负数,则从头开始删除
$pull:按照条件来删除所有匹配的元素
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 3, 2, 1, 4, 5, 5, 5 ] }
>db.product.update({"id":"2"},{$pull:{"scop":5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 3, 2, 1, 4 ] }
$:用来修改第一个匹配的元素D
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 3, 2, 1, 4 ] }
>db.product.update({"id":"2"},{$set:{"scop.0":5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{"_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 5, 2, 1, 4 ] }
>db.product.find({"id":"2"});
{ "_id": ObjectId("57182150560ca68a25e2991d"), "id" :"2", "name" : "毛笔","price" : 5, "scop" : [ 5, 2, 1, 4 ] }
>db.product.update({"id":"2","scop.1":2},{$set:{"scop.$":5}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 5, 5, 1, 4 ] }
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 5, 5, 1, 4 ] }
>db.product.update({"id":"2","scop.0":5},{$set:{"scop.$":6}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })
>db.product.find({"id":"2"});
{ "_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 5, 1, 4 ] }
h) 这边是update的第三个参数:upsert找到了符合条件的文档就更新,否则会以这个条件和更新文档来创建一个新文档。指定update方法的第三个参数为true(配置为1),如果为1,没有找到匹配则会新增。。。一般都设置为false(0)
i) Save方法 如果文档存在就更新,不存在就新建,主要根据”_id”来判断。
>db.product.find();
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
{"_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 5, 1, 4 ] }
{ "_id" :ObjectId("57182150560ca68a25e2991e"), "id" : "3","name" : "钢笔", "price" : 6 }
>db.product.save({"id":"4","name":"签字笔"});
WriteResult({ "nInserted" : 1 })
>db.product.find();
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
{"_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 5, 1, 4 ] }
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
{"_id" : ObjectId("57182ae9560ca68a25e29921"),"id" : "4", "name" : "签字笔" }
> db.product.save({"_id":ObjectId("57182ae9560ca68a25e29921"),"id":"4","name":"签字笔2"});
WriteResult({"nMatched" : 1, "nUpserted" : 0, "nModified" : 1})
>db.product.find();
{"_id" : ObjectId("57182150560ca68a25e2991c"),"id" : "1", "name" : "铅笔","price" : 4 }
{"_id" : ObjectId("57182150560ca68a25e2991d"),"id" : "2", "name" : "毛笔","price" : 5, "scop" : [ 6, 5, 1, 4 ] }
{"_id" : ObjectId("57182150560ca68a25e2991e"),"id" : "3", "name" : "钢笔","price" : 6 }
{ "_id" :ObjectId("57182ae9560ca68a25e29921"), "id" : "4","name" : "签字笔2" }
Save与insert的区别:
Insert纯插入,如果_id存在,则插入不成功;
Save更新或者插入,如果_id存在,则将进行数据更新操作,_id不存在则会新增一条数据,如果save的参数中未指定_id,则也会新增一条数据