MongoDB-Zero2One-Base-CRUD

15次阅读

共计 3786 个字符,预计需要花费 10 分钟才能阅读完成。

MongoDB CRUD Operations

CRUD operations create, read, update, and delete documents.
All write operations in MongoDB are atomic on the level of a single document.

MongoDB 中所有基于单个 document 的写操作都是原子性的

启动 MongoDB 服务,再通过 mongo 命令开启 Mongo Shell 操作数据库进行 CRUD

  • 对于 CRUD 的操作:https://docs.mongodb.com/manu…
  • 对于 Mongo Shell 提供的办法:https://docs.mongodb.com/manu…

Databases and Collections

MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases.

MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, though it contains more data types than JSON. For the BSON spec, see bsonspec.org. See also BSON Types.

MongoDB 的层级构造为 database(数据库)、collection(汇合)、document(文档)

  • collection 对应关系型数据库的表
  • document 对应关系型数据库的记录

 
db命令能够获取以后所应用的数据库,test 是连贯到 MongoDB 默认应用的库

> db
test

 
show dbs命令能够展现所有数据库(无数据的库不展现)

> show dbs
admin    0.000GB
config   0.000GB
local    0.000GB

 
use <db>命令能够抉择要应用的数据库,若库不存在则会主动创立

> use example
switched to db example
> db
example

Create Operations

https://docs.mongodb.com/manu…

MongoDB 提供了两种插入文档的办法,操作的 collection 如果不存在会主动创立

  • db.collection.insertOne():创立一条 document,对于 insertOne 的应用
  • db.collection.insertMany():创立多条 document,对于 insertMany 的应用

insertOne 和 insertMany 办法的应用

  • document:BSON documents,相似 JSON 但比 JSON 反对更多数据类型的格局,对于 BSON document
# 语法
db.collection.insertOne(<document>)
db.collection.insertMany(<document>)

# 插入一条数据
db.users.insertOne({name: "sue", age: 26, status: "pending"})

# 插入多条数据
db.users.insertMany([{name: "zhangsan", age: 23}, {name: "lisi", age: 24}])

插入数据胜利会返回相应的信息

  • acknowledged:确认机制,对于正确执行写入的操作 MongoDB 会返回 true
  • insertedId:MongoDB 为这一条插入的数据主动生成的惟一不反复的 ID

show collections命令能够展现以后数据库下的所有 collection

Read Operations

https://docs.mongodb.com/manu…

MongoDB 提供了 find 办法来查问文档

  • db.collection.find():查问 document,对于 find 的应用

find 办法的应用

  • filter:查问的过滤条件
  • projection:查问的字段(值为 1 示意查该字段,反之为 0),查问的字段不存在不会导致谬误
# 语法
db.collection.find(<filter>, <projection>)

# select * from users
db.users.find()
db.users.find({})

# select * from users limit 5
db.users.find().limit(5)

# select name, address from users where name = "bob" and age < 25
db.users.find({name: "bob", age: { $lt: 25}}, {name: 1, address: 1})

通过 $lt 这个 query operator 设置 < 条件,不应用 query operator 默认为 = 条件,对于更多 query operator 的应用

Update Operations

https://docs.mongodb.com/manu…

MongoDB 提供了三种更新文档的办法

  • db.collection.updateOne():更新一个 document,对于 updateOne 的应用
  • db.collection.updateMany():更新多个 document,对于 updateMany 的应用
  • db.collection.replaceOne():替换一个 document,对于 replaceOne 的应用

update:要更新的字段以及内容,语法如下(字段不存在时会创立字段),

updateOne 和 updateMany 办法的应用

  • filter:同查问一样的条件过滤
  • update:具体执行的更新操作(应用了 $set 这样的 update operator)对于 update operator 的应用
  • options:选项,具体详见文档
  • document:文档,应用 replaceOne 时需传入新的文档,新的文档如果附带_id 字段则该字段值必须和被替换的文档的_id 保持一致
# 语法
db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <document>, <options>)

# <update> 语法
{<update operator>: { <field1>: <value1>, ...},
    <update operator>: {<field2>: <value2>, ...},
    ...
}

# update users set name = "bob", age = 20 where age > 18 limit 1
db.users.updateOne({ age: { $gt: 18} },
    {$set: { name: "bob"},
        $set: {age: 20}
    }
)

# update users set name = "bob", age = 20 where age > 18
db.users.updateMany({ age: { $gt: 18} },
    {$set: { name: "bob"},
        $set: {age: 20}
    }
)

# replaceOne 相当于间接替换 document,仅放弃_id 统一
db.users.replaceOne({ age: { $gt: 18} },
    {name: "bob", age: 20}
)

Starting in MongoDB 4.2, MongoDB can accept an aggregation pipeline to specify the modifications to make instead of an update document. See the method reference page for details.

从 MongoDB4.2 开始,MongoDB 能够接管一个 aggregation pipeline 来进行指定的批改
对于 aggregation pipeline 的具体应用

Delete Operations

https://docs.mongodb.com/manu…

MongoDB 提供了两种删除文档的办法

  • db.collection.deleteOne():删除一个 document,对于 deleteOne 的应用
  • db.collection.deleteMany():删除多个 document,对于 deleteMany 的应用

deleteOne 和 deleteMany 办法的应用

  • filter:同查问一样的条件过滤
  • options:选项,具体详见文档
# 语法
db.collection.deleteOne(<filter>, <options>)
db.collection.deleteMany(<filter>, <options>)

# delete from users where status = "D" limit 1
db.inventory.deleteOne({status: "D"})

# delete from users where status = "D"
db.users.deleteMany({status: "D"})

# delete from users
db.users.deleteMany()
db.users.deleteMany({})

正文完
 0