zoukankan      html  css  js  c++  java
  • 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"})





  • 相关阅读:
    Python 自学笔记(二)
    Python 自学笔记(一)
    java.net.MalformedURLException: unknown protocol: 异常
    选择排序精简理解
    JAVA基于File的基本的增删改查
    Oracle常用操作表结构的语句
    jQuery
    基于jquery的ajax方法封装
    javascript运算符——条件、逗号、赋值、()和void运算符 (转载)
    javascript 闭包
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5216392.html
Copyright © 2011-2022 走看看