学习 MongoDB:查询文档 - 第一部分
由 Mux 赞助的 DEV 全球展示挑战赛:展示你的项目!
读取或查找文档是一项重要的操作。在 MongoDB 中,我们需要学习很多知识才能找到所需的数据。本文将介绍 MongoDB 提供的所有文档查询操作符。
目录:
阅读方法
在数据查询方面,我们有两种读取方法。
- 寻找
- 找到一个
在这篇文章中,我们将使用 `get_document()`,find因为两者的结果相同,唯一的区别是 `get_document()`findOne只返回匹配结果中的第一个文档。
我们将继续使用pokeworld上一篇文章中创建的数据库。
pokemons集合中的新文档结构
{
name: "pikachu",
type: "Electric",
stats: {
health: 40,
attack: 50,
defense: 45
},
level: 16,
weakness: "ground", // this field can be empty in some documents
evolution: "raichu",
moves: [
{name: "quick attack", dmg: 40},
{name: "thunder bolt", dmg: 90},
{name: "irontail", dmg: 50}
]
}
这份文档结构应该足以帮助我们学习不同的数据查询方法。让我们开始吧!
简单查询
# find grass type pokemon with level 20
> db.pokemons.find({type: "Grass", level: 40}).pretty()
查询嵌入式文档
嵌入式文档是指文档内部的对象,例如stats文档中的字段。使用引号和.点号可以查询嵌入式文档中的字段。
# find electric pokemon with health 40
> db.pokemons.find({type: "Electric", "stats.health": 40}).pretty()
pretty()该方法可帮助您在 shell 中格式化文档,使结果更易于阅读。
使用运算符查找文档
当需要更精细地查询数据时,简单的查询是不够的。MongoDB 提供了操作符,可以帮助我们构建更高级的查询。
运算符类型:
- 查询运算符(用于查找数据)
- 投影运算符(修改文档的呈现方式)
- 更新运算符(更新或添加文档数据)
本文将介绍查询运算符。我们可以使用不同类型的查询运算符。
- 比较运算符
- 逻辑运算符
- 元素查询运算符
- 评估查询运算符(下一篇文章)
- 地理空间算子(下一篇文章将介绍)
- 数组运算符(下一篇文章将介绍)
比较运算符
当我们想要比较文档中的数据,例如小于、大于、不等于时,我们会使用比较运算符。
- 运算符:
$eq,,,,,,,,$ne$gt$gte$lt$lte$in$nin
# $eq, no need in simple queries
> db.pokemons.find({$eq: {"name": "pikachu"}})
# $ne(not equal), get pokemons with health not equal to 40
> db.pokemons.find({"stats.attack": {$ne: 40}})
# $gt(greater), $gte(greater than equal),
# $lt(less than), $lte(less than equal)
> db.pokemons.find({type: "grass", "stats.health": {$gte: 40, $lt: 60}})
# $in(includes), $nin(not includes)
# find pokemons that have following healths: 40, 45, 50
> db.pokemons.find({"stats.health": {$in: [40, 50, 45]}})
逻辑运算符
逻辑运算符帮助我们构建逻辑语句,例如 AND、OR、NOR、NOT。
- 运算符:
$and,,,$or$nor$not
$or:当我们需要其中一个条件为真时,它非常有用。使用此运算符时,我们需要将两个或多个筛选条件放在一个数组中传递。
$nor:它与 $n 正好相反$or,并且可以以类似的方式使用,以执行与 $n 相反的操作。$or
# find pokemon with type grass or electric and health 40
> db.pokemons.find({
health: 40,
$or: [
{type: "grass"},
{type: "electric"}
]
})
$and:$and 还接受一个过滤器数组,并检查文档是否满足所有条件。
# find pokemons with health 40 and attack 60
> db.pokemons.find({
$and: [
{"stats.health": 40},
{"stats.attack": 60}
]
})
$not:当你想要否定一个过滤器时,即执行与过滤器相反的操作时,可以使用 $not。
# find pokemons with defense not greater than 45
> db.pokemons.find({ "stats.defense": {$not: {$gt: 45}} })
# you can use $eq operator with $not for "not equal" case.
元素运算符
这里有两个元素运算符。它们帮助我们处理字段而不是数据。我的意思是,例如,宝可梦文档中的字段,比如 `<field>` stats、 `<field>` 和 `<field>`。type
- 操作员:
$exists,$type
`$exists`:由于 MongoDB 文档的结构并不固定,某些文档可能会省略某些字段,因此我们有时需要检查某个字段是否存在于文档中。这时 `$exists`$exists操作符就派上用场了。
如果你查看我一开始给出的示例文档结构,你会发现weakness某些文档中该字段可能为空,也就是说,某些文档中根本不存在该字段(就像某些没有弱点的超级宝可梦一样 :p)。
# find pokemons who have weakness field/key
> db.pokemons.find({ weakness: {$exists: true}})
`$type`:此运算符用于检查文档中字段的类型。让我们来了解一下 MongoDB 中可用的类型,以便更好地使用此运算符。(我将列出一些重要且最常用的类型。完整列表请参见此处。)
- 数字类型:"int", "long", "decimal", "double"
- 布尔类型:"bool"
- 日期类型:“日期”、“时间戳”
- 字符串类型:"string"
- 对象类型:"对象"
- 数组类型:"数组"
- 对象 ID:"objectId"
- 空类型:"null"
- 二进制数据类型:"binData"
# Suppose you inserted a pokemon in collection
# Instead of numbers, you inserted them as string !
# let's find those docs !!
> db.pokemons.find({"stats.health": {$type: "string"}})
默认情况下,MongoDB 在存储数字时使用“double”类型。
我觉得内容很丰富。我们学习了很多在 MongoDB 中查询数据所需的重要且实用的操作符。下一篇文章,我将介绍其余的操作符(求值操作符和数组操作符)。
好了,今天就到这里!希望这个系列对你有帮助。
如果您有任何建议,欢迎在评论区告诉我 :)
下一篇文章: 查询文档 - 第二部分
上一篇: 基本 CRUD 操作
文章来源:https://dev.to/paras594/learn-mongodb-query-documents-i-309a