MongoDB数据库文档操作方法(必看篇)
前面的话
本文将详细介绍MongoDB数据库关于文档的增删改查
如果数据库中不存在集合,则MongoDB将创建此集合,然后将文档插入到该集合中

要在单个查询中插入多个文档,可以在<font size="+0">insert()</font>
命令中传递文档数组

可以使用js语法,插入多个文档

【save()】
插入文档也可以使用db.post.save(document)。 如果不在文档中指定_id,那么save()方法将与insert()方法一样自动分配ID的值。如果指定_id,则将以save()方法的形式替换包含_id的文档的全部数据。
也就是说save()方法和insert()方法的区别是,save()方法可以复写或修改,而insert()方法不可以
db.post.save(document)


【insertOne()】
使用<font size="+0">db.collection.insertOne()</font>
方法可以将单个文档插入到集合中

【insertMany()】
使用<font size="+0">db.collection.insertMany()</font>
方法可以将多个文档插入到集合中

查询文档
【find()】
要从MongoDB集合查询数据,需要使用MongoDB的<font face="NSimsun">find()</font>
方法,默认返回结果中的前20条文档,输入"it"显示接下来的20条文档。
<font size="+0">find()</font>
命令的基本语法如下:
db.COLLECTION_NAME.find(document)
<font size="+0">find()</font>
方法将以非结构化的方式显示所有文档

可以限定查询条件

可以通过find 的第二个参数来指定返回的键,值为1或true表示显示该键,值为0或false表示不显示该键

find()方法下的count()方法可以显示符合条件的文档数量

【findOne()】
<font size="+0">findOne()</font>
方法只返回一个文档,该文档是最早被添加的文档

【比较操作符】
小于 {<key>:{$lt:<value>}} 小于或等于 {<key>:{$lte:<value>}} 大于 {<key>:{$gt:<value>}} 大于或等于 {<key>:{$gte:<value>}} 不等于 {<key>:{$ne:<value>}} 等于 {<key>:{$eq:<value>}}
取得x小于2的值

取得x大于等于2的值

取得x不等于2的值

【逻辑操作符】
可以使用逻辑操作符$and、$or来表示与、或
{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] } { $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }
【正则表达式】
文档查询可以使用正则表达式,但只支持字符串类型的数据

【$where】
$where操作符功能强大而且灵活,它可以使用任意的JavaScript作为查询的一部分,包含JavaScript表达式的字符串或者JavaScript函数

使用字符串

使用函数

限制与跳过
【limit()】
如果需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数
默认返回结果中的前20条文档,输入"it"显示接下来的20条文档
如果没有指定limit()方法中的参数则显示集合中的所有数据
db.COLLECTION_NAME.find().limit(NUMBER)

【skip()】
可以使用skip()方法来跳过指定数量的数据,skip方法同样接受一个数字参数作为跳过的记录条数
db.COLLECTION_NAME.find().skip(NUMBER)

排序
【sort()】
在MongoDB中使用sort()方法对数据进行排序,sort()方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而-1是用于降序排列
db.COLLECTION_NAME.find().sort({KEY:1})

更新文档
MongoDB 使用update()或save()方法来更新集合中的文档
【update()】
update() 方法用于更新已存在的文档。语法格式如下:
db.collection.update(<query>,<update>,{upsert:<boolean>, multi: <boolean>,writeConcern:<document>})
query : update的查询条件,类似sql update查询内where后面的 update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的 upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入 multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新 writeConcern :可选,抛出异常的级别
[注意]经过测试,upsert参数无法设置为true或者false,都可以插入新的字段

mongodb默认只更新找到的第一条记录,将x:1,更新为x:10

要特别注意的是,如果不使用$set,则将文档的内容替换为x:10

更新全部记录,将x:10,更新为x:1

mongodb默认只添加到更新找到的第一条记录,将x:1的记录,添加z:1

将找到的x:2的全部记录,添加z:2

【save()】
save()方法可以插入或更新文档,如果参数中的文档的_id与集合中所存在的_id都不同,则插入;如果相同,则更新

删除文档
MongoDB remove()函数是用来移除集合中的数据
【remove()】
默认地,mongodb删除符合条件的所有文档
db.collection.remove(<query>,{justOne: <boolean>, writeConcern: <document>})
query :删除的文档的条件。 justOne : (可选)如果设为 true 或 1,则只删除一个文档。 writeConcern :(可选)抛出异常的级别。
只删除符合条件的第一个文档

删除符合条件的所有文档

插入文档
要将数据插入到 MongoDB 集合中,需要使用 MongoDB 的 insert()或save()方法,还有insertOne()或insertMany()方法
【insert()】
insert()命令的基本语法如下
db.COLLECTION_NAME.insert(document)
在插入的文档中,如果不指定_id参数,那么 MongoDB 会为此文档分配一个唯一的ObjectId。_id为集合中的每个文档唯一的12个字节的十六进制数