接上一篇:
#删除数据
*删除某条数据@
> db.things.remove({i:1});
> db.things.find({i:1});
> db.things.find({i:2});
{ "_id" : ObjectId("4ee8510a0c16000000006ec7"), "i" : 2, "i2" : 4 }
{ "_id" : ObjectId("4ee8514d0c16000000006edb"), "i" : 2, "i2" : 4, "i3" : 8 }
db.things.remove()会删除things聚集中的所有数据。
甚至可以删除聚集本身@
db.things.drop()
#更新数据
*mongodb官方建议,如果仅仅是更改某些域,那么使用$modifiers 更加合适~
写道
The modifier update also features operation atomicity and very little network data transfer.
*modifiers有如下操作@
写道
$set 赋值
$unset 删除某个列
$push 追加到原有值后
$pushAll 追加数组到原有值后
$addToSet and $each 向数组添加一个元素/向数组添加多个元素
$pop 移除某个数组元素(-1表示第一个、1表示最后一个)
$pull 移除指定的数组元素
$pullAll 移除多个指定的数组元素
$rename 更新域名
$bit 按位操作,仅限整型
*将客户joy的年龄+1@
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "name" : "joy", "age" : 22 }
> db.customers.update({name:"joy"},{$inc:{age:1}});
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "name" : "joy", "age" : 23 }
再添加两条数据@
db.customers.save({name:"joy",age:23});db.customers.save({name:"joy",age:23});
如何将所有joy的年龄都变为24呢?
试一下前面的方法@
> db.customers.update({name:"joy"},{$inc:{age:1}});
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "name" : "joy", "age" : 24 }
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "name" : "joy", "age" : 23 }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "name" : "joy", "age" : 23 }
很遗憾只改变了第一条数据:(
update的语法为:db.customers.update(query, object[, upsert_bool, multi_bool])
,有两个可选参数,我们没有用到,官网上对这几个参数的解释是@
写道
object 需要更新的域
upsert 如果为true则有记录时更新记录,没有记录时添加一条新记录(默认值似乎是true)
multi 决定到底是更新所有查询到的记录还是只更新一条(默认值似乎是false)
再试一下@
> db.customers.update({name:"joy"},{$inc:{age:1}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "name" : "joy", "age" : 25 }
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "name" : "joy", "age" : 24 }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "name" : "joy", "age" : 24 }
所有名字叫joy的客户的年龄都增大了一岁。
现在把25岁的joy改成24岁,可以这么来@
> db.customers.update({name:"joy",age:25},{$set:{age:24}});
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "name" : "joy", "age" : 24 }
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "name" : "joy", "age" : 24 }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "name" : "joy", "age" : 24 }
这样在customers中就有三个名字和年龄都相同的客户了:)
添加一个性别域@
> db.customers.update({name:"joy",age:24},{$set:{gender:"m"}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 24, "gender" : "m", "name" : "joy" }
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 24, "gender" : "m", "name" : "joy" }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "gender" : "m", "name" : "joy" }
删除该域@
> db.customers.update({name:"joy",age:24},{$unset:{gender:"m"}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 24, "name" : "joy" }
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 24, "name" : "joy" }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy" }
经过一些步骤,现在数据库中3个joy的年龄不同了,接下来给他们加一个数组域,记录他们的订单记录@
> db.customers.update({name:"joy"},{$push:{orders:"1000"}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : [ "1000" ] }
再试试$pushAll@
> db.customers.update({name:"joy",age:22},{$pushAll:{orders:["1001","1002"]}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : [ "1000", "1001", "1002" ] }
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : [ "1000" ] }
试试$addToSet and $each@
> db.customers.update({name:"joy",age:22},{$addToSet:{orders:{$each:["1003","1004"]}}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : [ "1000", "1001", "1002", "1003", "1004" ] }
$pop把数组元素弹出@
> db.customers.update({name:"joy",age:22},{$pop:{orders:1}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : [ "1000", "1001", "1002", "1003" ] }
> db.customers.update({name:"joy",age:22},{$pop:{orders:-1}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : [ "1001", "1002", "1003" ] }
$push也可以把数组元素弹出@
> db.customers.update({name:"joy",age:22},{$pull:{orders:"1002"}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : [ "1001", "1003" ] }
> db.customers.update({name:"joy",age:22},{$pullAll:{orders:["1001","1003"]}},true,true);
> db.customers.find({name:"joy"});
{ "_id" : ObjectId("4ee99ce18b12000000004f86"), "age" : 23, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee99d0f8b12000000004f87"), "age" : 24, "name" : "joy", "orders" : [ "1000" ] }
{ "_id" : ObjectId("4ee98be10b240000000027f3"), "age" : 22, "name" : "joy", "orders" : [ ] }
把orders修改为customer_orders@
> db.customers.update({name:"joy"},{$rename:{orders:"customer_orders"}},true,true);
Invalid modifier specified $rename
很遗憾:(查了资料才发现$rename这个命令在Version 1.7.2+ 以后才有,而$bit这个命令Version 1.7.5+之后才有。我的测试版本@
> db.version();
1.6.5
*最后一点,关于$的用法:$相当有用,官网的解释是@
写道
$指的就是在一次查询中匹配的数组项的位置,通过使用$可以找到数组元素并操作它。(翻译的不好,满头大汗:|)
定义一个文章+评论的数据模式@
> archive = {title:"mongodb learning",author:"someone",comments:[{by:"joy",votes:1},{by:"tim",votes:3}]}
{
"title" : "mongodb learning",
"author" : "someone",
"comments" : [
{
"by" : "joy",
"votes" : 1
},
{
"by" : "tim",
"votes" : 3
}
]
}
> db.archives.insert(archive);
> db.archives.find();
{ "_id" : ObjectId("4ee9ad938b12000000004f88"), "title" : "mongodb learning", "author" : "someone", "comments" : [ { "by" : "joy", "votes" : 1 }, { "by" : "tim", "votes" : 3 } ] }
将joy的评论votes加1@
> db.archives.update({'comments.by':"joy"},{$inc:{'comments.$.votes':1}},false,true);
> db.archives.find({author:"someone"});
{ "_id" : ObjectId("4ee9ad938b12000000004f88"), "title" : "mongodb learning", "author" : "someone", "comments" : [ { "by" : "joy", "votes" : 2 }, { "by" : "tim", "votes" : 3 } ] }