MongoDB——文档增删改查命令使用

MongoDB

文档增删改查

命令操作 描述
db.collection.insert() db.collection.insert() 将单个文档或多个文档插入到集合中
db.collection.insertOne() 插入文档,3.2 版中的新功能
db.collection.insertMany() 插入多个文档,3.2 版中的新功能
db.collection.update 更新或替换与指定过滤器匹配的单个文档,或更新与指定 过 滤 器 匹 配 的 所 有 文 档 。 默 认 情 况 下 ,db.collection.update()方法更新单个文档。要更新多个文档,请使用 multi 选项。
db.collection.updateOne(<filter>,<update>, <options>) 即使多个文档可能与指定的过滤器匹配,最多更新与指定的过滤器匹配的单个文档。 3.2 版中的新功能
db.collection.updateMany(<filter>,<update>, <options>) 更新所有与指定过滤器匹配的文档。 3.2 版中的新功能
db.collection.replaceOne(<filter>,<update>, <options>) 即使多个文档可能与指定过滤器匹配,也最多替换一个与指定过滤器匹配的文档。
db.collection.remove() 删除单个文档或与指定过滤器匹配的所有文档
db.collection.deleteOne() 即使多个文档可能与指定过滤器匹配,也最多删除一个与指定过滤器匹配的文档。 3.2 版中的新功能
db.collection.deleteMany() 删除所有与指定过滤器匹配的文档。 3.2 版中的新功能
db.collection.find(query,projection) 查询文档
db.collection.findOne()

文档插入

sh 复制代码
 db.col.insert({title:'高数', description: '极限', by: '圆老师', url: 'www.gaoshu.com', tags: ['difficult', 'yue'], likes: 100})
WriteResult({ "nInserted" : 1 })

查看

sh 复制代码
> db.col.find()
{ "_id" : ObjectId("5d80715e019abe974dac5164"), title:'高数', description: '极限', by: '圆老师', url: 'www.gaoshu.com', tags: ['difficult', 'yue'], likes: 100}

更新文档

插入

js 复制代码
db.inventory.insertMany( [
 { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
 { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
 { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
 { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
 { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
 { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }])

为了更新文档,MongoDB 提供了更新操作符(例如$set)来修改字段值。

要使用更新运算符,请将以下形式的更新文档传递给更新方法:

js 复制代码
{
 <update operator>: { <field1>: <value1>, ... },
 <update operator>: { <field2>: <value2>, ... },
 ...
}

如果字段不存在,则某些更新操作符(例如$set)将创建该字段。

下面的示例在 inventory 集合上使用 db.collection.updateOne()方法更新项目等于" paper"的

第一个文档:

js 复制代码
db.inventory.updateOne(
 { item: "paper" },
 { 
 	$set: { "size.uom": "cm", status: "P" },
 	$currentDate: { lastModified: true }
 }
)
  • 使用$set 运算符将 size.uom 字段的值更新为" cm",将状态字段的值更新为" P",

  • 使用currentDate 运算符将 lastModified 字段的值更新为当前日期。 如果 lastModified字段不存在,则currentDate 将创建该字段。

更新多个文档则将条件改成范围查询即可

查询

MongoDB 查询文档使用 find() 方法。find() 方法以非结构化的方式来显示所有文档

js 复制代码
db.collection.find(query, projection)
  • query :可选,使用查询操作符指定查询条件
  • projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值
js 复制代码
db.collection.find({"item":"aa"}, {"_id":1, "status":1})

如果你需要以易读的方式来读取数据,可以使用 pretty() 方法。

js 复制代码
db.col.find().pretty()

指定条件查询

js 复制代码
db.inventory.find( {} ) //所有文档
db.inventory.find( { status: "D" } ) //指定等于条件
db.inventory.find( { status: { $in: [ "A", "D" ] } } )   // 或条件
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )  // 或条件
db.inventory.find( { status: "A", qty: { $lt: 30 } } )  // and 条件
db.inventory.find( {
 status: "A",
 $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} ) // and + or 条件

其他查询参考https://www.mongodb.com/docs/manual/tutorial/query-documents/

删除文档

在执行 remove()函数前先执行 find()命令来判断执行的条件是否正确,这是一个比较好的习惯。

js 复制代码
db.collection.remove(
	 <query>,
	 {
	 justOne: <boolean>,
	 writeConcern: <document>
	 }
)
  • query :(可选)删除的文档的条件。参考查询里query写法
  • justOne : (可选)如果设为 true 或 1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。
  • writeConcern :(可选)抛出异常的级别。

删除所有数据

js 复制代码
db.inventory.deleteMany({})

即使从集合中删除所有文档,删除操作也不会删除索引。

相关推荐
小吴编程之路1 天前
MySQL 索引核心特性深度解析:从底层原理到实操应用
数据库·mysql
~莫子1 天前
MySQL集群技术
数据库·mysql
凤山老林1 天前
SpringBoot 使用 H2 文本数据库构建轻量级应用
java·数据库·spring boot·后端
就不掉头发1 天前
Linux与数据库进阶
数据库
与衫1 天前
Gudu SQL Omni 技术深度解析
数据库·sql
咖啡の猫1 天前
Redis桌面客户端
数据库·redis·缓存
oradh1 天前
Oracle 11g数据库软件和数据库静默安装
数据库·oracle
what丶k1 天前
如何保证 Redis 与 MySQL 数据一致性?后端必备实践指南
数据库·redis·mysql
_半夏曲1 天前
PostgreSQL 13、14、15 区别
数据库·postgresql
把你毕设抢过来1 天前
基于Spring Boot的社区智慧养老监护管理平台(源码+文档)
数据库·spring boot·后端