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默认应用的库
> dbtest
show dbs
命令能够展现所有数据库(无数据的库不展现)
> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GB
use <db>
命令能够抉择要应用的数据库,若库不存在则会主动创立
> use exampleswitched to db example> dbexample
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 usersdb.users.find()db.users.find({})# select * from users limit 5db.users.find().limit(5)# select name, address from users where name = "bob" and age < 25db.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 1db.users.updateOne( { age: { $gt: 18 } }, { $set: { name: "bob" }, $set: { age: 20 } })# update users set name = "bob", age = 20 where age > 18db.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 1db.inventory.deleteOne({ status: "D" })# delete from users where status = "D"db.users.deleteMany({ status: "D" })# delete from usersdb.users.deleteMany()db.users.deleteMany({})