学习 MongoDB:CRUD 基础知识
这是我上一篇博文的续篇,之前我讲解了如何安装和设置 MongoDB,以及一些基本命令。在这篇文章中,我将讲解基本的 CRUD 操作。那就打开你的 shell,开始吧!
目录:
使用数据库
数据库将包含您的所有集合(SQL 中的表)。我们将创建一个名为pokeworld
本文的数据库,它将包含pokemons
诸如trainers
等集合。
创建数据库
> use pokeworld
检查正在使用的数据库
> db
#or
> db.getName()
删除数据库
> db.dropDatabase()
获取数据库统计信息
> db.stats()
获取数据库中集合的信息
> db.getCollectionInfos()
使用集合
集合将文档保存在一起。它们类似于 SQL 中的表。例如,就像在 SQL 中一样,我们可以有一个包含姓名、年龄、性别、职业等字段的用户表。我们可以创建一个用户集合,其中的文档(类似 JSON)也可以以键值对的形式包含这些字段。
示例文档:
{
name: "John",
age: 20,
gender: "male",
occupation: "engineer"
}
CRUD 操作也基于集合进行。所以,从现在开始,我们的大部分工作都将围绕集合展开。我们将在pokeworld
数据库中创建一些集合,并学习一些与它们相关的实用方法。
创建收藏夹
在 mongo 中,向不存在的集合添加数据时会自动创建集合。但要显式创建集合,请使用以下方法:
> db.createCollection("pokemons")
在数据库中显示集合
> show collections
# or
> db.getCollectionNames()
删除集合
> db.pokemons.drop()
重命名集合
> db.pokemons.renameCollection("pokemons-renamed")
检查收藏统计数据
> db.pokemons.stats()
统计集合中的文档数量
> db.pokemons.count()
CRUD 操作
我们所需的所有创建、读取、更新和删除操作都可以在集合上使用。让我们看看您需要的基本和最常用的操作。
创造
我们需要两种方法来在集合中创建文档。
- 插入一个
- 插入多个
为什么不将数据添加到数据库pokemons
的集合中pokeworld
。
# insert one pokemon
> db.pokemons.insertOne({
name: "Pikachu",
type: "Electric",
health: 35
})
# insert more than one pokemon at once
> db.pokemons.insertMany([
{ name: "Squirtle", type: "Water", health: 44 },
{ name: "Poliwag", type: "Water", health: 40 },
{ name: "Bulbasaur", type: "Grass", health: 45 },
{ name: "Pidgey", type: "Flying", health: 40 }
])
读
要从集合中读取数据,我们只需要两种方法。这些方法包含多个运算符和选项,可以帮助我们根据需要过滤数据。
- 查找:返回多个文档
- findOne:仅返回第一个文档
在这两个操作中,我们可以传递过滤器。如果没有传递过滤器,则在 的情况下返回所有文档,find
在 的情况下返回第一个文档。fineOne
# find one
> db.pokemons.findOne({ name: "Pikachu" })
# find all water type pokemons
> db.pokemons.find({ type: "Water" })
# find pokemons with health greater than 40
> db.pokemons.find({ health: { $gt: 40 } })
# find pokemons with type water and health greater than 40
> db.pokemons.find({ type: "Water", health: { $gt: 40 }})
在上面的例子中,$gt
我们使用了一个运算符。还有其他运算符,我们将在后续的文章中讨论。
更新
再次,我们有两种更新文档的方法
- 更新一
- updateMany
两者都接受一个过滤器(类似于 find 函数)来查找需要更新的文档,以及一个定义要更新内容的对象。我们使用$set
操作符来更新文档中的字段。
# update one pokemon
> db.pokemons.updateOne(
{ name: "Pikachu" },
{ $set: { health: 45 }}
)
# update multiple pokemons and add weakness to water types
> db.pokemons.updateMany(
{ type: "Water" },
{ $set: { weakness: "Electric" }}
)
# increment health of pikachu by 10 and also add weakness
> db.pokemons.updateOne(
{ name: "Pikachu" },
{
$inc: { health: 10 },
$set: { weakness: "Ground" }
}
)
在上面的例子中,您可以看到,我们可以在一次更新中更改多个字段。$inc
是一个您可以在这里看到的运算符,我们可以用来增加/减少Number
类型。还有其他运算符,我们稍后会讨论。
删除
我们的最后一个操作是删除。
- 删除一个
- 删除多个
这两个方法与 update 和 find 方法类似。它们使用过滤器来识别我们要删除的文档。我们来看一些例子:
# delete one pokemon
> db.pokemons.deleteOne({ name: "Pikachu" })
# delete pokemons with health greater than or equal to 40
> db.pokemons.deleteMany({ health: { $gte: 40 }})
# delete all pokemons !!
> db.pokemons.deleteMany({})
伤心 :( 我们删除了皮卡丘。别忘了把他加回来。
但这就是删除方法的工作原理。如您所见,它类似于查找一个或多个文档。如果能找到它,就可以删除它。在上面的例子中,我们使用了一个运算符$gte
。它代表大于等于。您可以看到它是如何用于查询数据的。
好了,本篇就到这里。接下来,我们将深入探讨读取操作。更多数据过滤方法、可用的操作符等等。
祝您编码愉快 :)
下一篇: 查询文档 - 第一部分
上一篇: 入门
鏂囩珷鏉ユ簮锛�https://dev.to/paras594/learn-mongo-crud-basics-5bpk