当前位置:网站首页>《MongoDB入门教程》第08篇 比较运算符
《MongoDB入门教程》第08篇 比较运算符
2022-07-26 05:25:00 【不剪发的Tony老师】
本文将会介绍 MongoDB 中查找文档时常用的一些比较运算符,包括 $eq、$gt、$gte、$lt、$lte、$ne、$in 以及 $nin。
$eq 运算符
$eq 运算符用于匹配字段等于(=)指定值的文档。$eq 运算符的语法如下:
{
<field>: {
$eq: <value> } }
以上语法等价于下面的写法:
{
<field>: <value>}
我们创建一个集合 products 作为下文中的演示:
db.products.insertMany([
{
"_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : {
"ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]},
{
"_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : {
"ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]},
{
"_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : {
"ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]},
{
"_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : {
"ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]},
{
"_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : {
"ram" : 4, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}
])
以下示例使用 $eq 运算符查找 products 集合中 price 字段等于 899 的所有文档:
db.products.find({
price: {
$eq: 899
}
}, {
name: 1,
price: 1
})
我们也可以使用以下等价写法:
db.products.find({
price: 899
}, {
name: 1,
price: 1
})
以上两个示例的返回结果相同:
[
{
_id: 2, name: 'xTablet', price: 899 },
{
_id: 3, name: 'SmartTablet', price: 899 }
]
下面的示例使用 $eq 运算符查找嵌套文档 spec 中字段 ram 的值等于 4 的文档:
db.products.find({
"spec.ram": {
$eq: 4
}
}, {
name: 1,
"spec.ram": 1
})
查询返回的文档如下:
[
{
_id: 1, name: 'xPhone', spec: {
ram: 4 } },
{
_id: 5, name: 'SmartPhone', spec: {
ram: 4 } }
]
接下来的示例使用 $eq 运算符查找 products 集合中数组 color 包含“black”元素的文档:
db.products.find({
color: {
$eq: "black"
}
}, {
name: 1,
color: 1
})
查询返回的文档如下:
[
{
_id: 1, name: 'xPhone', color: [ 'white', 'black' ] },
{
_id: 2, name: 'xTablet', color: [ 'white', 'black', 'purple' ] }
]
$gt 运算符
$gt 运算符用于匹配字段大于(>)指定值的文档。$gt 运算符的语法如下:
{
field: {
$gt: value}}
以下示例使用 $gt 运算符查找集合 products 中 price 大于 699 的文档:
db.products.find({
price: {
$gt: 699
}
}, {
name: 1,
price: 1
})
查询返回的结果如下:
[
{
_id: 1, name: 'xPhone', price: 799 },
{
_id: 2, name: 'xTablet', price: 899 },
{
_id: 3, name: 'SmartTablet', price: 899 }
]
$gte 运算符
$gte 运算符用于匹配字段大于等于(>=)指定值的文档。$gte 运算符的语法如下:
{
field: {
$gte: value} }
以下示例使用 $gte 运算符查找集合 products 中嵌入文档 spec 的字段 screen 大于或者等于 9.5 的文档:
db.products.find({
"spec.screen": {
$gte: 9.5
}
}, {
name: 1,
"spec.screen": 1
})
返回结果如下:
[
{
_id: 2, name: 'xTablet', spec: {
screen: 9.5 } },
{
_id: 3, name: 'SmartTablet', spec: {
screen: 9.7 } },
{
_id: 4, name: 'SmartPad', spec: {
screen: 9.7 } },
{
_id: 5, name: 'SmartPhone', spec: {
screen: 9.7 } }
]
$lt 运算符
$lt 运算符用于匹配字段小于(<)指定值的文档。$lt 运算符的语法如下:
{
field: {
$lt: value} }
以下示例使用 $lt 运算符查找集合 products 中数组字段 storage 至少包含一个小于 128 的元素的文档:
db.products.find({
storage: {
$lt: 128
}
}, {
name: 1,
storage: 1
})
查询返回的文档如下:
[
{
_id: 1, name: 'xPhone', storage: [ 64, 128, 256 ] },
{
_id: 3, name: 'SmartTablet', storage: [ 16, 64, 128 ] }
]
$lte 运算符
$lte 运算符用于匹配字段小于等于(<=)指定值的文档。$lte 运算符的语法如下:
{
field: {
$lte: value} }
以下示例使用 $lte 运算符查找集合 products 中发布日期早于或者等于 2015-01-11 的所有文档:
db.products.find({
"releaseDate": {
$lte: new ISODate('2015-01-01')
}
}, {
name: 1,
releaseDate: 1
});
查询返回的文档如下:
[
{
_id: 1,
name: 'xPhone',
releaseDate: ISODate("2011-05-14T00:00:00.000Z")
},
{
_id: 2,
name: 'xTablet',
releaseDate: ISODate("2011-09-01T00:00:00.000Z")
}
]
$ne 运算符
$ne 运算符匹配字段不等于(<>)指定值的文档。$ne 运算符的语法如下:
{
field: {
$ne: value}}
以下示例使用 $ne 运算符查找集合 products 中 price 不等于 899 的文档:
db.products.find({
price: {
$ne: 899
}
}, {
name: 1,
price: 1
})
查询返回的文档如下:
[
{
_id: 1, name: 'xPhone', price: 799 },
{
_id: 4, name: 'SmartPad', price: 699 },
{
_id: 5, name: 'SmartPhone', price: 599 },
{
_id: 6, name: 'xWidget' }
]
$in 运算符
$in 运算符匹配字段等于(=)数组中任意值的文档。$in 运算符的语法如下:
{
field: {
$in: [<value1>, <value2>,...] }}
如果 field 只有一个值,$in 运算符匹配该字段等于数组中任意值的文档。如果 field 也是一个数组,$in 运算符匹配该数组包含数组 [value1, value2,…] 中任意值的文档。
数组列表 value1, value2, … 可以是一个常量列表或者正则表达式列表。
正则表达式是一组定义搜索模式的字符,例如正则表达式 /\d+/ 匹配任何数组,包括1,123,1234 等。
以下示例使用 $in 运算符查找 products 集合中 price 字段等于 599 或者 799 的文档:
db.products.find({
price: {
$in: [699, 799]
}
}, {
name: 1,
price: 1
})
查询返回的文档如下:
[
{
_id: 1, name: 'xPhone', price: 799 },
{
_id: 4, name: 'SmartPad', price: 699 }
]
$nin 运算符
$nin 运算符匹配字段不等于(!=)数组中任意值的文档,或者指定字段不存在的文档。$nin 运算符的语法如下:
{
field: {
$nin: [ <value1>, <value2> ...]} }
以下示例使用 $nin 运算符查找 products 集合中 price 字段既不等于 599 也不等于 799 的文档:
db.products.find({
price: {
$nin: [699, 799]
}
}, {
name: 1,
price: 1
})
查询返回的文档如下:
[
{
_id: 2, name: 'xTablet', price: 899 },
{
_id: 3, name: 'SmartTablet', price: 899 },
{
_id: 5, name: 'SmartPhone', price: 599 }
]
以下示例使用 $nin 运算符查找 color 数组字段中不包含任何匹配正则表达式 /^g+/ 或者 /^w+/ 的元素的文档:
db.products.find({
color: {
$nin: [/^g+/, /^w+/]
}
}, {
name: 1,
color: 1
})
查询返回的文档如下:
[ {
_id: 3, name: 'SmartTablet', color: [ 'blue' ] } ]
边栏推荐
- High frequency electronic circuit review examination questions and answers
- 代码审计之百家cms
- ALV入门
- Getaverse, a distant bridge to Web3
- Leetcode linked list problem - 203. remove the linked list elements (learn the linked list by one question and one article)
- 10. Regular expression matching
- Mongondb API usage
- C语言详解系列——函数的认识(3)形参,实参,嵌套调用和链式访问
- SAP报表开发步骤
- Teach you how to use code to realize SSO single sign on
猜你喜欢

Common modules in ansible

DOM操作--操作节点

OD-Paper【2】:Fast R-CNN

Chinese character style transfer --- learn the conversion and generation of one to many programmed Chinese characters through generation confrontation network

Use flutter to adjust a color filter for the picture of my little sister

推荐必读:测试人员如何快速熟悉新业务?

LeetCode链表问题——203.移除链表元素(一题一文学会链表)

IVR在voip电话系统的应用与价值

Development to testing: a six-year road to automation from scratch

Hack The Box - Introduction to Networking Module详细讲解中文教程
随机推荐
ALV入门
High frequency electronic circuit review examination questions and answers
LeetCode链表问题——203.移除链表元素(一题一文学会链表)
SAP报表开发步骤
学生如何申请免费IDEA
测试必备工具之Fiddler,你真的了解吗?
高频电子线路复习考试题及答案
87. Disturb string
Hack The Box -SQL Injection Fundamentals Module详细讲解中文教程
Yolov5 implementation process - Directory
Map making of environmental impact assessment based on remote sensing interpretation and GIS technology
C language - Advanced pointer
Development to testing: a six-year road to automation from scratch
Recommend 12 academic websites for free literature search, and suggest to like and collect!
家居vr全景展示制作提高客户转化
unity场景跳转脚本
OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation
Chinese character style transfer --- learn the conversion and generation of one to many programmed Chinese characters through generation confrontation network
MySQL optimization
It's enough for newcomers to learn how to do functional tests