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({})

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

相关推荐
月光水岸New2 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
狄加山6752 小时前
数据库基础1
数据库
我爱松子鱼2 小时前
mysql之规则优化器RBO
数据库·mysql
chengooooooo2 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
万事可爱^3 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
Rverdoser3 小时前
【SQL】多表查询案例
数据库·sql
Galeoto3 小时前
how to export a table in sqlite, and import into another
数据库·sqlite
人间打气筒(Ada)4 小时前
MySQL主从架构
服务器·数据库·mysql
leegong231114 小时前
学习PostgreSQL专家认证
数据库·学习·postgresql
喝醉酒的小白4 小时前
PostgreSQL:更新字段慢
数据库·postgresql