当前位置:网站首页>Mongodb tutorial Chapter 08 comparison operators
Mongodb tutorial Chapter 08 comparison operators
2022-07-26 05:30:00 【Teacher Tony who doesn't cut his hair】
This article will introduce MongoDB Some comparison operators commonly used to find documents in , Include $eq、$gt、$gte、$lt、$lte、$ne、$in as well as $nin.
$eq Operator
$eq Operator is used to match fields equal to (=) The document with the specified value .$eq The syntax of the operator is as follows :
{
<field>: {
$eq: <value> } }
The above grammar is equivalent to the following writing :
{
<field>: <value>}
Let's create a collection products As a demonstration in the following :
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]}
])
The following example uses $eq Operator to find products Collection price Field equals 899 All documents of :
db.products.find({
price: {
$eq: 899
}
}, {
name: 1,
price: 1
})
We can also use the following equivalent expression :
db.products.find({
price: 899
}, {
name: 1,
price: 1
})
The return results of the above two examples are the same :
[
{
_id: 2, name: 'xTablet', price: 899 },
{
_id: 3, name: 'SmartTablet', price: 899 }
]
The following example uses $eq Operator to find nested documents spec Middle field ram The value is equal to the 4 Documents :
db.products.find({
"spec.ram": {
$eq: 4
}
}, {
name: 1,
"spec.ram": 1
})
The documents returned from the query are as follows :
[
{
_id: 1, name: 'xPhone', spec: {
ram: 4 } },
{
_id: 5, name: 'SmartPhone', spec: {
ram: 4 } }
]
The following example uses $eq Operator to find products Array in the collection color contain “black” The document of the element :
db.products.find({
color: {
$eq: "black"
}
}, {
name: 1,
color: 1
})
The documents returned from the query are as follows :
[
{
_id: 1, name: 'xPhone', color: [ 'white', 'black' ] },
{
_id: 2, name: 'xTablet', color: [ 'white', 'black', 'purple' ] }
]
$gt Operator
$gt Operator is used to match fields larger than (>) The document with the specified value .$gt The syntax of the operator is as follows :
{
field: {
$gt: value}}
The following example uses $gt Operator to find a set products in price Greater than 699 Documents :
db.products.find({
price: {
$gt: 699
}
}, {
name: 1,
price: 1
})
The result of the query is as follows :
[
{
_id: 1, name: 'xPhone', price: 799 },
{
_id: 2, name: 'xTablet', price: 899 },
{
_id: 3, name: 'SmartTablet', price: 899 }
]
$gte Operator
$gte Operator is used to match fields greater than or equal to (>=) The document with the specified value .$gte The syntax of the operator is as follows :
{
field: {
$gte: value} }
The following example uses $gte Operator to find a set products Embed documents in spec Field of screen Greater than or equal to 9.5 Documents :
db.products.find({
"spec.screen": {
$gte: 9.5
}
}, {
name: 1,
"spec.screen": 1
})
The results are as follows :
[
{
_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 Operator
$lt Operator is used to match fields less than (<) The document with the specified value .$lt The syntax of the operator is as follows :
{
field: {
$lt: value} }
The following example uses $lt Operator to find a set products Array fields in storage At least one less than 128 The document of the element :
db.products.find({
storage: {
$lt: 128
}
}, {
name: 1,
storage: 1
})
The documents returned from the query are as follows :
[
{
_id: 1, name: 'xPhone', storage: [ 64, 128, 256 ] },
{
_id: 3, name: 'SmartTablet', storage: [ 16, 64, 128 ] }
]
$lte Operator
$lte Operator is used to match fields less than or equal to (<=) The document with the specified value .$lte The syntax of the operator is as follows :
{
field: {
$lte: value} }
The following example uses $lte Operator to find a set products The release date of is earlier than or equal to 2015-01-11 All documents of :
db.products.find({
"releaseDate": {
$lte: new ISODate('2015-01-01')
}
}, {
name: 1,
releaseDate: 1
});
The documents returned from the query are as follows :
[
{
_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 Operator
$ne Operator matching field is not equal to (<>) The document with the specified value .$ne The syntax of the operator is as follows :
{
field: {
$ne: value}}
The following example uses $ne Operator to find a set products in price It's not equal to 899 Documents :
db.products.find({
price: {
$ne: 899
}
}, {
name: 1,
price: 1
})
The documents returned from the query are as follows :
[
{
_id: 1, name: 'xPhone', price: 799 },
{
_id: 4, name: 'SmartPad', price: 699 },
{
_id: 5, name: 'SmartPhone', price: 599 },
{
_id: 6, name: 'xWidget' }
]
$in Operator
$in Operator matching field equals (=) Documents with arbitrary values in the array .$in The syntax of the operator is as follows :
{
field: {
$in: [<value1>, <value2>,...] }}
If field Only one value ,$in Operator matches a document whose field is equal to any value in the array . If field It's also an array ,$in Operator matches that the array contains an array [value1, value2,…] Document with any value in .
Array list value1, value2, … It can be a list of constants or a list of regular expressions .
A regular expression is a set of characters that define a search pattern , For example, regular expressions /\d+/ Match any array , Include 1,123,1234 etc. .
The following example uses $in Operator to find products Collection price Field equals 599 perhaps 799 Documents :
db.products.find({
price: {
$in: [699, 799]
}
}, {
name: 1,
price: 1
})
The documents returned from the query are as follows :
[
{
_id: 1, name: 'xPhone', price: 799 },
{
_id: 4, name: 'SmartPad', price: 699 }
]
$nin Operator
$nin Operator matching field is not equal to (!=) Documents with arbitrary values in the array , Or specify a document where the field does not exist .$nin The syntax of the operator is as follows :
{
field: {
$nin: [ <value1>, <value2> ...]} }
The following example uses $nin Operator to find products Collection price Field is neither equal to 599 Also is not equal to 799 Documents :
db.products.find({
price: {
$nin: [699, 799]
}
}, {
name: 1,
price: 1
})
The documents returned from the query are as follows :
[
{
_id: 2, name: 'xTablet', price: 899 },
{
_id: 3, name: 'SmartTablet', price: 899 },
{
_id: 5, name: 'SmartPhone', price: 599 }
]
The following example uses $nin Operator to find color The array field does not contain any matching regular expressions /^g+/ perhaps /^w+/ The document of the element :
db.products.find({
color: {
$nin: [/^g+/, /^w+/]
}
}, {
name: 1,
color: 1
})
The documents returned from the query are as follows :
[ {
_id: 3, name: 'SmartTablet', color: [ 'blue' ] } ]
边栏推荐
- MongonDB API使用
- Migrate the server and reconfigure the database (the database has no monitoring, and the monitoring starts with tns-12545, tns-12560, tns-00515 errors)
- A trick to teach you to easily understand Potter's map
- 怎么办理聚合收款码
- Week 6 Learning Representation: Word Embedding (symbolic →numeric)
- 电机控制专栏文章汇总
- SSH Remote Management
- 项目版本号怎么命名?看起来牛B
- FPGA question brushing sequence detection
- QT writing IOT management platform 47 general database settings
猜你喜欢

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

Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images

Hack The Box - Introduction to Networking Module详细讲解中文教程

Leetcode linked list problem - 203. remove the linked list elements (learn the linked list by one question and one article)

OD-Paper【2】:Fast R-CNN

Leetcode linked list problem - 206. reverse linked list (learn linked list by one question and one article)

Embedded development notes, practical knowledge sharing

MongonDB API使用

Week 6 Learning Representation: Word Embedding (symbolic →numeric)

No background, no education? Is it really hopeless for specialist testers to enter Internet factories?
随机推荐
C language function
Basic methods of realizing licensing function in C language
SSTI payload and various bypass methods
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
Application and value of IVR in VoIP telephone system
OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation
ALV report flow diagram
调试利器!一款轻量级日志库 log.c
高频电子线路复习考试题及答案
Usage and common problems of SIP softphone registered with SIP account
Do you really understand fiddler, a necessary tool for testing?
动态内存管理及柔性数组
MySQL optimization
87. Disturb string
nn.Moudle模块-创建神经网络结构需要注意的细节
How students apply for free idea
LNMP architecture
Attack and defense world -- easy_ web
TZC 1283: simple sort Bubble Sort
Getting started with ALV