MongoDB常用语句小册

MongoDB常用语句小册

最近MongoDB使用的比较多,但是毕竟不像MySQL这种用了好多年的数据库,语句信手拈来,有的时候还是需要想一想才能写出来,于是把常用的CRUD语句整理一下,方便查阅。

1、插入语句

格式

-- 插入一条数据,返回id
db.collection.insertOne(
   <document>
)

-- 插入多条数据,返回id列表
db.collection.insertMany(
[<document>, <document>]
)

-- 插入一条或多条数据,返回插入数量
db.collection.insert(
)
  1. 集合不存在,则创建集合
  2. insertOne和insertMany插入成功会返回成功结果和自动生成的id

img

  1. insert返回的是插入的记录数nInserted,不返回具体的id。
  • 插入一个文档,返回:
WriteResult({ "nInserted" : 1, "writeConcernError" : [ ] })
  • 插入多个文档,返回:
BulkWriteResult({
        "nRemoved" : 0,
        "nInserted" : 2,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "writeErrors" : [ ]
})

例子

比如我要创建一个表:book,则插入语句为:

db.book.insertOne({
  "title": "mongodb删库到跑路",
  "publishVersion": 1,
  "price": 66.88,
  "author": {
    "name": "小白"
  },
  "comments": [
    {
      "userName": "小白",
      "comment": "很好,讲的很透彻",
      "score": 4.5
    },
    {
      "userName": "老黑",
      "comment": "差评,没跑成",
      "score": 0.5
    }
  ]
})

2、查询语句

格式

-- 1.查询全部文档,等价于 select * from collection
db.collection.find(
)

-- 2.指定条件查询,等价于 select * from collection where field1=value1 and ……
db.collection.find(
{ <field1>: <value1>, ... }
)

-- 3.指定条件模糊查询,等价于 select * from collection where field1 like "%value1%" and ……注意/是放在最外面
db.collection.find(
{ <field1>: /<value1>/, ... }
)

-- 4.指定条件嵌套查询,需要用引号括起来
db.collection.find(
{ "<field1>. <fieldA>": <value1>, ... }
)

-- 5.使用运算符查询,运算符列表:https://docs.mongodb.com/master/reference/operator/query/
db.collection.find(
{ <field1>: { <operator1>: <value1> }, ... }
)

-- 6.查询指定字段,value为1时则为返回的字段。0则强制为不返回id(因为id会默认返回)。如果都为0,则返回除此以外的其他字段。
db.collection.find({}, {
    <field1>: 1,
    _id: 0
})

-- 7.返回指定字段也可以用嵌套方式返回
db.collection.find({}, {
    "<field1>. <fieldA>": 1,
    _id: 0
})
-- 或者
db.collection.find({}, {
    <field1> : {
         <fieldA> : 1
    },
    _id: 0
})

-- 8.查询数组中的文档,这里要注意,数组中的文档要完全匹配才能命中查询
db.collection.find({
    <arrayfield> : {
         <fieldA> : <value1>
    }
})

例子

我们用上面创建的book作为示例,进行查询。以下的查询都能命中book表里的这条数据。

-- 1.查询全部文档,等价于 select * from book
db.book.find(
)

-- 2.指定条件查询,等价于 select * from collection where title = 'mongodb删库到跑路' and publishVersion = 2
db.book.find(
    {
        title: "mongodb删库到跑路",
        publishVersion: 1
    }
)

-- 3.指定条件模糊查询,等价于 select * from collection where title like "%删库%"
db.book.find(
    {
        title: /删库/
    }
)

-- 4.指定条件嵌套查询,嵌套的字段需要用引号括起来
db.book.find(
{ "author.name": "小白"}
)

-- 5.in查询
db.book.find({
    publishVersion: {
        $in: [1]
    }
})

-- 6.大于小于查询
db.book.find({
    price: {
        $lt: 80
    }
})

-- 7.or查询
db.book.find({
    $or: [{
        title: /删库到跑路/
    }, {
        price: {
            $lt: 80
        }
    }]
})
-- 8.查询指定字段,value为1时则为返回的字段。0则强制为不返回id(因为id会默认返回)。如果都为0,则返回除此以外的其他字段。
db.book.find({}, {
    title: 1,
    price: 1,
    "author.name": 1,
    _id: 0
})

-- 9.查询数组中的文档
db.book.find({
    "comments": {
        "userName": "小白",
        "comment": "很好,讲的很透彻",
        "score": 4.5
    }
})

3、更新语句

格式

-- 1.更新单个文档,也就是更新符合<filter>条件的第一个文档
db.collection.updateOne(<filter>, <update>, <options>)
-- 2.更新多个文档,也就是更新符合<filter>条件的全部文档
db.collection.updateMany(<filter>, <update>, <options>)
-- 3.更换文档,也就是符合<filter>条件的第一个文档,替换为<update>中的文档
db.collection.replaceOne(<filter>, <update>, <options>)

<filter>,是查询条件

<update>中,使用更新操作符(例如$set)来修改字段值。如果是replaceOne,则不能使用更新操作符,直接就是一个文档。

<options>可加可不加。

例子

-- 1.更新单个文档的值。等价于:update book set price = 88.88 where title = 'mongodb删库到跑路'
db.book.updateOne({
    title: "mongodb删库到跑路"
}, {
    $set: {
        price: 88.88
    }
})
-- 2.直接更换文档。(示例相当于直接删除了comments部分)
db.book.replaceOne({
    title: "mongodb删库到跑路"
}, {
    "title": "mongodb删库到跑路",
    "publishVersion": 1,
    "price": 66.88,
    "author": {
        "name": "小白"
    }
})

4、删除语句

格式

-- 删除第一条符合查询条件的文档
db.collection.deleteOne(<filter>)
-- 删除全部符合查询条件的文档
db.collection.deleteMany(<filter>)
-- 删除匹配的文档
db.collection.remove(<filter>)

注意,filter使用与读取操作相同的语法,所以一切参考查询即可。

其实和更新一样,分为只命中第一条和命中全部符合条件的数据。

删除操作不会删除索引。

例子

db.book.deleteOne({
    title: "mongodb删库到跑路"
})
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白码上飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值