【mongodb】基本语法

插入

在已有的文档中插入一组新值

db.col.update({ },{$set:{“xxx”:“xxxx”}})

效果

> db.col.find().pretty()
{
        "_id" : ObjectId("6244268e153444102d14ece9"),
        "title" : "MongoDB 教程",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "菜鸟教程",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL",
                "xxxx"
        ],
        "likes" : 100,
        "test" : "xxxx"
}
{ "_id" : ObjectId("624427281f98d57822e9031c"), "test5" : "OK" }
> db.col.update({ "_id" : ObjectId("6244268e153444102d14ece9")},{$set:{"xxx":"xxxx"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.col.find().pretty()
{
        "_id" : ObjectId("6244268e153444102d14ece9"),
        "title" : "MongoDB 教程",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "菜鸟教程",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL",
                "xxxx"
        ],
        "likes" : 100,
        "test" : "xxxx",
        "xxx" : "xxxx"
}
{ "_id" : ObjectId("624427281f98d57822e9031c"), "test5" : "OK" }
>

如需插入到数组中

使用addToSet

db.col.update({ },{$set:{“addToSet”:“xxxx”}})
效果

> db.col.update({ "_id" : ObjectId("6244268e153444102d14ece9")},{$addToSet:{"tags":"addToSet"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.col.find().pretty()
{
        "_id" : ObjectId("6244268e153444102d14ece9"),
        "title" : "MongoDB 教程",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "菜鸟教程",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL",
                "xxxx",
                "addToSet"
        ],
        "likes" : 100,
        "test" : "xxxx",
        "xxx" : "xxxx"
}
{ "_id" : ObjectId("624427281f98d57822e9031c"), "test5" : "OK" }

或使用push

db.col.update({ },{$push:{“xxx”:“xxxx”}})

效果

> db.col.update({ "_id" : ObjectId("6244268e153444102d14ece9")},{$push:{"tags":"push"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.col.find().pretty()
{
        "_id" : ObjectId("6244268e153444102d14ece9"),
        "title" : "MongoDB 教程",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "菜鸟教程",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL",
                "xxxx",
                "addToSet",
                "push"
        ],
        "likes" : 100,
        "test" : "xxxx",
        "xxx" : "xxxx"
}
{ "_id" : ObjectId("624427281f98d57822e9031c"), "test5" : "OK" }

添加用户

db.createUser({ user: 'testname', // 用户名 pwd: 'testpwd', // 密码 roles: [ { role: 'readWrite', // 读写权限 db: 'test' // 所属数据库 } ] })
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值