码迷,mamicode.com
首页 > 数据库 > 详细

mongodb创建、更新、删除

时间:2015-03-13 14:24:28      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:mongodb

1.插入操作

user = {"username":"lcq","sex":"man"}
db.user.insert(user)




2.更新
var lcq = db.user.findOne({"username":"lcq"}}
lcq.address = {"province":"shandong","location":"zoupin"}
lcq.haha = "hehhehe"
db.user.update({"username":"lcq"},lcq)


> db.user.update({"username":"lcq"},{$set:{"test":"test"}})
> db.user.findOne({"username":"lcq"})
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "haha" : "afdfdfd",
        "sex" : "man",
        "test" : "test",
        "username" : "lcq"
}



> db.user.update({"username":"lcq"},{$unset:{"test":1}})


$unset删除某个字段


> lcq = db.user.findOne({"username":"lcq"})
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "haha" : "afdfdfd",
        "sex" : "man",
        "username" : "lcq"
}
>
>
> lcq.count = 1
> db.user.update({"username":"lcq"},lcq)


> db.user.update({"username":"lcq"},{$inc:{"count":2}})



$inc设置某个字段自增


> db.user.update({"username":"lcq"},{$set:{"friends":['zhangsan']}})


> db.user.findOne()
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "count" : 3,
        "friends" : [
                "zhangsan"
        ],
        "haha" : "afdfdfd",
        "sex" : "man",
        "username" : "lcq"
}


> db.user.update({"username":"lcq"},{$push:{"friends":'lisi'}})
>
> db.user.findOne()
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "count" : 3,
        "friends" : [
                "zhangsan",
                "lisi"
        ],
        "haha" : "afdfdfd",
        "sex" : "man",
        "username" : "lcq"
}



$push向数组中添加新的值

> db.user.update({"username":"lcq"},{$addToSet:{"friends":'lisi'}})
>
> db.user.findOne()
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "count" : 3,
        "friends" : [
                "zhangsan",
                "lisi"
        ],
        "haha" : "afdfdfd",
        "sex" : "man",
        "username" : "lcq"
}


> db.user.update({"username":"lcq"},{$addToSet:{"friends":'xiaoming'}})
>
>
> db.user.findOne()
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "count" : 3,
        "friends" : [
                "zhangsan",
                "lisi",
                "xiaoming"
        ],
        "haha" : "afdfdfd",
        "sex" : "man",
        "username" : "lcq"
}
>


可见使用$addToSet添加时,如果存在相同的值则不添加。


> db.user.update({"friends":"lisi"},{$set:{"friends.$":"join"}})
>
>
>
> db.user.findOne()
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "count" : 3,
        "friends" : [
                "zhangsan",
                "join",
                "xiaoming"
        ],
        "haha" : "afdfdfd",
        "sex" : "man",
        "username" : "lcq"
}


使用$符进行数组的定位,实现更新数组特定位置的值。

> lcq = db.user.findOne()
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "count" : 3,
        "friends" : [
                "zhangsan",
                "join",
                "xiaoming"
        ],
        "haha" : "afdfdfd",
        "sex" : "man",
        "username" : "lcq"
}
>
>
> lcq.count = 10
10
>


> db.user.save(lcq)
>


> db.user.findOne()
{
        "_id" : ObjectId("55024de2849b70a4f670f413"),
        "address" : {
                "province" : "shandong",
                "location" : "zoupin"
        },
        "count" : 10,
        "friends" : [
                "zhangsan",
                "join",
                "xiaoming"
        ],
        "haha" : "afdfdfd",
        "sex" : "man",
        "username" : "lcq"
}



save方法在不存在时创建,存在时更新。更加方便。


3.删除

> db.user.insert({"username":"zhangsan"})
> db.user.remove({"username":"zhangsan"})





mongodb创建、更新、删除

标签:mongodb

原文地址:http://blog.csdn.net/liuchangqing123/article/details/44240417

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!