mongodb 相关的查找,插入删除等操作

http://blog.csdn.net/mcpang/article/details/7833805

1.find()/findOne()
mongodb数据库的查询操作即使用find()或者findOne()函数,也可根据不同的条件进行查询。查询的写法(以find()为例)可以如下:
db.A.find()/db.A.find({})

"{}"是否省略不影响查询,都表示查找集合A下的所有文档。也可以以文档做条件:db.A.find({"a":1,"b":1}),其中查找同时满足属
性a等于1且属性b也等于1的文档,若需要满足属性c等于1,可直接添加到文档中:db.A.find({"a":1,"b":1,"c":1})。

2.指定键的设置
指定键的设置即展示,比如某集合有10个属性,经查询后只关心集合中文档的某几个属性。如下文档:
{ "_id" : ObjectId("5018da521781352fe25bf4d2"), "a" : "1", "b" : "1", "c" : "1", "d" : "1", "e" : "1" }
只关系属性a,b,c可如下设置:
db.A.find({},{"a,":1,"b":1,"c":,"_id":0})
这里的1和0与文档中的键值是不同意义的,这里的1:表示显示,0:表示不显示。其中"_id"键默认存在的,需要显示设置。

结果如下:
{ "a" : "1", "b" : "1", "c" : "1" }

 

--------------------------------------条件查询可类比结构化查询语句SQL--------------------------------------------
3.条件查询
3.1 条件操作符
"$lt"===================>"<"
"$lte"==================>"<="
"$gt"===================>">"
"$gte"==================>">="
"$ne"===================>"!="

如:某集合B集合中文档有属性x值为整数,需查找10<x<=30的文档,写法如下:
db.B.find({"x":{"$gt":10,"$lte":30}})

如:从某集合B中查找日期属性day值大于2012/01/01的文档数据,写法如下:
db.B.find({"day":{"$gt":new Date("2012/01/01")}})
适合于需要进行日调度、月调度、周调度数据等业务处理范围的场合。

3.2 $in包含/$nin不包含
$in:查询匹配指定条件值的文档;
$nin:查询不匹配指定条件值的文档;

SQL:写法:字段 in ('值1','值1'.....)
mongodb:db.B.find({"x":{"$in":['值1','值2',.....]}})

SQL:写法:字段 not in ('值1','值1'.....)
mongodb:db.B.find({"x":{"$nin":['值1','值2',.....]}})

$in/$nin优点:可指定不同类型条件和值。

3.3 $or或查询

$or:查询匹配多个条件多个值的文档;

SQL:写法:字段1 = 'xxx' or 字段2 in ( 'xxx').....
mongodb:db.B.find({"$or":[{"x":{"$in":['值1','值2'...]}},{"y":"3"}]})

3.4 $all匹配所有

比如文档:
{"name":jack,"age":[1,2,3]}
{"name":jack,"age":[1,4,3]}

db.B.find({"age":{"$all":[2,3]}})结果:{"name":jack,"age":[1,2,3]}

3.5 $exists 判断文档属性是否存在

db.B.find({"name":{"$exists":true}})   --查找属性name存在的文档
db.B.find({"name":{"$exists":false}})  --查找属性name不存在的文档

3.6 属性值为null情况
如下操作并可知道:
> db.C.find()
{ "_id" : ObjectId("5018fccd1781352fe25bf511"), "a" : "14", "b" : "14" }
{ "_id" : ObjectId("5018fccd1781352fe25bf512"), "a" : "15", "b" : "15" }
{ "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null }
> db.C.find({"c":null})
{ "_id" : ObjectId("5018fccd1781352fe25bf511"), "a" : "14", "b" : "14" }
{ "_id" : ObjectId("5018fccd1781352fe25bf512"), "a" : "15", "b" : "15" }
{ "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null }
可见查询属性c值为null文档,包括属性c值为null、该属性c不存在两个部分。若想只查询属性c为null的文档
如下:
> db.C.find({"c":{"$in":[null],"$exists":true}})
{ "_id" : ObjectId("5018fccd1781352fe25bf510"), "a" : "13", "b" : "13", "c" : null }

3.7 $not元条件句

可与其他条件配合使用,即不在匹配范围之内的文档,下面可见其用法。

3.8 $mod取模运算

db.B.find({"age":{"$mod":[5,1]}}) --表示查找年龄/5余1的所有文档

若查找年龄/5余1之外的所有文档,可结合$not运算:
db.B.find({"age":{"$not":{"$mod":[5,1]}}})

3.9 正则表达式

db.B.find({"name":/jack/i})

3.10 $size

> db.C.find()
{ "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 1, 2, 3 ] }
{ "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] }
> db.C.find({"b":{"$size":2}})
{ "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] }

3.11 $slice

返回数组的一个子集,即对以某属性为基础,返回多少条(范围)。也可以接受偏移值和要返回的元素数量,来返回中间的结果。
> db.C.find()
{ "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 1, 2, 3 ] }
{ "_id" : ObjectId("501e71607d4bd700257d8a42"), "a" : "1", "b" : [ 1, 2 ] }
> db.C.findOne({},{"b":{"$slice":[2,3]}})
{ "_id" : ObjectId("501e71557d4bd700257d8a41"), "a" : "1", "b" : [ 3 ] }
> db.C.findOne({},{"b":{"$slice":-2}})
{
        "_id" : ObjectId("501e71557d4bd700257d8a41"),
        "a" : "1",
        "b" : [
                2,
                3
        ]
}

3.12 $where

即可执行任务javascript作为查询的一部分。
$where的值可以是function、也可以是字符串等等。

db.C.find({"$where":function(){return this.a == "1"}})与db.C.find({"$where":"this.a == '1'"}})

注意:采用$where子句查询在速度上较常规查询慢的多。因文档需要从BSON转换成javascript对象,然后通过"$where"的表达式来运行。
      不用利用索引。可用常规查询做前置过滤,配置"$where"查询进行调优,可达到不牺牲性能的要求。


4 游标

使用游标返回find的执行结果,客户端对游标的实现通常能对结果进行数量的限制、略过部分结果、排序等有效控制。

var cursor = db.C.find()     --定义游标
while(cursor.hasNext()){
 var obj = cursor.next();
 print(obj.a);
 ......
}

db.C.find().limit(10)  --限制查询的结果条数为10条
db.C.find().skip(10)   --忽略匹配的前10条,显示从第11条匹配的文档开始所有的文档
db.C.find().sort({"a":-1})  --sort以键/值,表示按某个属性进行排序,1:升序,-1:降序

高级查询选项:
$maxscan:integer  --指定查询最多扫描的文档数量
$min:document     --查询的开始条件
$max:document     --查询的结束条件
$hint:document    --指定服务器使用哪个索引进行查询
$explain:boolean  --获取查询执行的细节(用到的索引、结果数量、耗时等),而并非真正执行查询
$snapshot:boolean --确保查询的结果是在查询执行那一刻的一致快照


mongodb_修改器($inc/$set/$unset/$push/$pop/upsert......)

http://blog.csdn.net/mcpang/article/details/7752736

对于文档的更新除替换外,针对某个或多个文档只需要部分更新可使用原子的更新修改器,能够高效的进行文档更新。更新修改器是中特殊的键,
用来指定复杂的操作,比如增加、删除或者调整键,还可能是操作数组或者内嵌文档。

1.$inc
--------------------------------------------------------------------------
这个修改器干什么使的呢?看看下面示例的具体操作后的结果即可知道。

示例文档:{"uid":"201203","type":"1",size:10}

> db.b.insert({"uid":"201203","type":"1",size:10})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 10 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : 1}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 11 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : 2}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 13 }
> db.b.update({"uid" : "201203"},{"$inc":{"size" : -1}})
> db.b.find()
{ "_id" : ObjectId("5003b6135af21ff428dafbe6"), "uid" : "201203", "type" : "1",
"size" : 12 }

得出结论:修改器$inc可以对文档的某个值为数字型(只能为满足要求的数字)的键进行增减的操作。
(这里有个问题:上篇中说到更新默认只对满足条件的记录集中第一个文档进行更新,那么使用$inc修改器之后,还是一样吗?)

2.$set
-------------------------------------------------------------------
用来指定一个键并更新键值,若键不存在并创建。来看看下面的效果:

> db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "sname" : "jk", "type" : "3", "uid" : "20120002" }
--size键不存在的场合
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"size":10}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "jk", "type" : "3", "uid" : "20120002" }
--sname键存在的场合
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":"ssk"}})
> db.a.find()
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "ssk", "type" : "3", "uid" : "20120002" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num"
: 50, "sname" : "jk", "type" : "1", "uid" : "20120002" }
--可改变键的值类型
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":["java",".net","c++"]}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "desc" : "hello world2!",
        "num" : 40,
        "size" : 10,
        "sname" : [
                "java",
                ".net",
                "c++"
        ],
        "type" : "3",
        "uid" : "20120002"
}

对于内嵌的文档,$set又是如何进行更新的内嵌的文档的呢,请看下面的示例:
示例文档:{"name":"toyota","type":"suv","size":{"height":10,"width":5,"length":15}}

> db.c.findOne({"name":"toyota"})
{
        "_id" : ObjectId("5003be465af21ff428dafbe7"),
        "name" : "toyota",
        "type" : "suv",
        "size" : {
                "height" : 10,
                "width" : 5,
                "length" : 15
        }
}
> db.c.update({"name":"toyota"},{"$set":{"size.height":8}})
> db.c.findOne({"name":"toyota"})
{
        "_id" : ObjectId("5003be465af21ff428dafbe7"),
        "name" : "toyota",
        "type" : "suv",
        "size" : {
                "height" : 8,
                "width" : 5,
                "length" : 15
        }
}
> db.c.update({"name":"toyota"},{"$set":{"size.width":7}})
> db.c.findOne({"name":"toyota"})
{
        "_id" : ObjectId("5003be465af21ff428dafbe7"),
        "name" : "toyota",
        "type" : "suv",
        "size" : {
                "height" : 8,
                "width" : 7,
                "length" : 15
        }
}
可见:对于内嵌文档在使用$set更新时,使用"."连接的方式。


3.$unset
----------------------------------------------------------------
从字面就可以看出其意义,主要是用来删除键。
示例操作效果如下:

> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"sname":1}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "desc" : "hello world2!",
        "num" : 40,
        "size" : 10,
        "type" : "3",
        "uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"num":0}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "desc" : "hello world2!",
        "size" : 10,
        "type" : "3",
        "uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"size":-1}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "desc" : "hello world2!",
        "type" : "3",
        "uid" : "20120002"
}
> db.a.update({"uid" : "20120002","type" : "3"},{"$unset":{"desc":"sssssss"}})
> db.a.findOne({"uid" : "20120002","type" : "3"})
{
        "_id" : ObjectId("500216de81b954b6161a7d8f"),
        "type" : "3",
        "uid" : "20120002"
}

得出结论:使用修改器$unset时,不论对目标键使用1、0、-1或者具体的字符串等都是可以删除该目标键。

4.数组修改器--$push
------------------------------------------------------------------
示例操作效果如下:
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "type" : "suv",
"size" : { "height" : 8, "width" : 7, "length" : 15 } }

--先push一个当前文档中不存在的键title
> db.c.update({"name" : "toyota"},{$push:{"title":"t1"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1" ], "type" : "suv" }
 
--再向title中push一个值
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2" ], "type" : "suv" }

--再向title中push一个值
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }

--再向一个已经存在的键值非数组类型的键push一个值
> db.c.update({"name" : "toyota"},{$push:{"size.height":10}})
Cannot apply $push/$pushAll modifier to non-array
> db.c.update({"name" : "toyota"},{$push:{"name":"ddddddd"}})
Cannot apply $push/$pushAll modifier to non-array

得出结论:$push--向文档的某个数组类型的键添加一个数组元素,不过滤重复的数据。添加时键存在,要求键值类型必须是数组;键不存在,则创建数组类型的键。

5.数组修改器--$ne/$addToSet
---------------------------------------------------------------------
主要给数组类型键值添加一个元素时,避免在数组中产生重复数据,$ne在有些情况是不通行的。

> db.c.update({"title" : {$ne:"t2"}},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }

> db.c.update({"name" : "toyota"},{$addToSet:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }

6.数组修改器--$pop、$pull
------------------------------------------------------------
$pop从数组的头或者尾删除数组中的元素,示例如下:
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3", "t4" ],"type" : "suv" }

--从数组的尾部删除 1
> db.c.update({"name" : "toyota"},{$pop:{"title":1}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3" ], "type" : "suv" }
--从数组的头部 -1
> db.c.update({"name" : "toyota"},{$pop:{"title":-1}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t2", "t3" ], "type" : "suv" }
--从数组的尾部删除 0
> db.c.update({"name" : "toyota"},{$pop:{"title":0}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t2" ], "type" : "suv" }
 
$pull从数组中删除满足条件的元素,示例如下:
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2", "t3" ],"type" : "suv" }
 
> db.c.update({"name" : "toyota"},{$pull:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t3" ], "type" : "suv" }
 
7.数组的定位修改器
-------------------------------------------------------------------
在需要对数组中的值进行操作的时候,可通过位置或者定位操作符("$").数组是0开始的,可以直接将下标作为键来选择元素。
示例如下:
{"uid":"001",comments:[{"name":"t1","size":10},{"name":"t2","size":12}]}

> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 10 }, { "name" : "t2", "size" : 12 } ] }
> db.c.update({"uid":"001"},{$inc:{"comments.0.size":1}})
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 11 }, { "name" : "t2", "size" : 12 } ] }
> db.c.update({"comments.name":"t1"},{$set:{"comments.$.size":1}})
> db.c.find({"uid":"001"})
{ "_id" : ObjectId("5003da405af21ff428dafbe8"), "uid" : "001", "comments" : [ {
"name" : "t1", "size" : 1 }, { "name" : "t2", "size" : 12 } ] }

--若为多个文档满足条件,则只更新第一个文档。

8.upsert
-----------------------------------------------------------------
upsert是一种特殊的更新。当没有符合条件的文档,就以这个条件和更新文档为基础创建一个新的文档,如果找到匹配的文档就正常的更新。
使用upsert,既可以避免竞态问题,也可以减少代码量(update的第三个参数就表示这个upsert,参数为true时)

> db.c.remove()
> db.c.update({"size":11},{$inc:{"size":3}})
> db.c.find()
> db.c.update({"size":11},{$inc:{"size":3}},false)
> db.c.find()
> db.c.update({"size":11},{$inc:{"size":3}},true)
> db.c.find()
{ "_id" : ObjectId("5003ded6c28f67507a6df1de"), "size" : 14 }

9.save函数
-----------------------------------------------------------------
1.可以在文档不存在的时候插入,存在的时候更新,只有一个参数文档。
2.要是文档含有"_id",会调用upsert。否则,会调用插入。
> db.a.find()
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num": 50,
 "sname" : "jk", "type" : "1", "uid" : "20120002" }
> var o = db.a.findOne()
> o.num = 55
55
> db.a.save(o)
> db.a.find()
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num": 55,
 "sname" : "jk", "type" : "1", "uid" : "20120002" } 

mongodb_创建、删除、更新

Mongodb(版本:mongodb-win32-i386-2.0.6)数据库对数据同样可以进行增删改查的操作,如下:
1.创建新增操作

db.a.insert({"name":"jk"})
--insert函数,直接将文档做参数,保存到集合a中。

2.删除操作

db.a.remove()
--删除a集合中的所有文档,但不删除集合本身,原有的索引也保留。

db.a.remove({"name":"jk"})
--删除a集合中含有name为jk的所有文档。
--支持以一个查询文档作为可选参数,可删除满足该参数的所有文档
--删除数据时永久性的,不能撤销,也不能恢复。

db.a.drop()
--删除a集合,包括a集合中所有文档。

3.更新文档

1>.修改的目标文档
2>.修改器 描述对找到的文档做哪些修改
3>.更新操作是原子的:若两个更新同时发生,先到达服务器的先执行,接着执行另外一个。因此相互有冲突的更新可以火速传递,并不会相互干扰;最后的更新会取得“胜利”。

==============================单个文档更新替换====================

针对模式结构发生较大变化场合,将下面文档1:
{"uid":"20120001","type":"1","num":30,"desc":"hello world!"}

修改成以下文档2:
{"uuid":"20120001",
 "utext":{
  "type":"1","num":30,"desc":"hello world!"
 }
}

具体的操作如下:
> db.a.insert({"uid":"20120001","type":"1","num":30,"desc":"hello world!"})
> db.a.findOne()
{ "_id" : ObjectId("5002112a81b954b6161a7d8d"), "uid" : "20120001", "type" : "1"
, "num" : 30, "desc" : "hello world!" }
>var obj = db.a.findOne({"uid":"20120001"})
>obj.utext={"type":"1","num":30,"desc":"hello world!"}
{ "type" : "1", "num" : 30, "desc" : "hello world!" }
> obj.uuid = obj.uid
20120001
>delete obj.type
true
>delete obj.num
true
>delete obj.desc
true
>delete obj.uid
true
>db.a.update({"uid":"20120001"},obj)
> db.a.findOne()
{
        "_id" : ObjectId("5002112a81b954b6161a7d8d"),
        "utext" : {
                "type" : "1",
                "num" : 30,
                "desc" : "hello world!"
        },
        "uuid" : "20120001"
}
-------------------------------------可能出现的问题------------------------------------
update是首先查询目标文档,然后在进行替换更新。若查询的时候存在多个目标文档的情况下,情况又会如何呢?。看下面的例子:

1>.当更新的对象是更新时查找出的第一条记录情况:
保存如下文档:
{"uid":"20120002","type":"2","num":40,"desc":"hello world1!"}
{"uid":"20120002","type":"1","num":50,"desc":"hello world2!"}

> db.a.find({"uid":"20120002"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "uid" : "20120002", "type" : "2"
, "num" : 40, "desc" : "hello world2!" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "uid" : "20120002", "type" : "1"
, "num" : 50, "desc" : "hello world1!" }

> var p = db.a.findOne({"uid":"20120002","type":"2"})
> p.type="3"
3
> db.a.update({"uid":"20120002"})
> db.a.find()
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "uid" : "20120002", "type" : "3"
, "num" : 40, "desc" : "hello world2!" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "uid" : "20120002", "type" : "1"
, "num" : 50, "desc" : "hello world1!" }

更新结果:update时将查找出满足条件的第一条记录进行更新,其他记录不变。

2>.当更新的对象是不是更新时查找出的第一条记录情况:
接上例继续:
> var o = db.a.findOne({"uid":"20120002","type":"1"})
> o.type="4"
4
> db.a.update({"uid":"20120002"},o)
cannot change _id of a document old:{ _id: ObjectId('500216de81b954b6161a7d8f'),
 uid: "20120002", type: "3", num: 40.0, desc: "hello world2!" } new:{ _id: Objec
tId('50026affdeb4fa8d154f8572'), uid: "20120002", type: "4", num: 50.0, desc: "h
ello world1!" }

更新结果:update更新第一条时o的"_id"与第二条记录的"_id"重复而出错。
=====================单个文档更新替换=====================

=====================多个文档更新替换=====================
默认情况下,更新只能对符合条件的第一个文档进行操作。多文档更新主要涉及update方法的第四个参数,默认为false,若设置为true,即进行多文档更新。

将所有uid为20120002的文档增加一个属性为sname值为jk:
> db.a.find()
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "uid" : "20120002", "type" : "3"
, "num" : 40, "desc" : "hello world2!" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "uid" : "20120002", "type" : "1"
, "num" : 50, "desc" : "hello world1!" }

> db.a.update({"uid":"20120002"},{$set:{"sname":"jk"}},false,true)

> db.a.find()
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "sname" : "jk", "type" : "3", "uid" : "20120002" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num"
: 50, "sname" : "jk", "type" : "1", "uid" : "20120002" }
==========================多个文档更新替换======================
























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值