mongodb 中的 not in 比较运算符
比较运算符在处理大型数据集时非常有用。从数据中获得洞察力是有帮助的。
本文介绍如何在 mongodb 中使用 $nin
(not in
) 比较运算符。
它还展示了如何在 mongodb 中将 $nin
与 find()
和 update()
方法一起使用。此外,我们还将学习使用 $nin
运算符的正则表达式。
mongodb 中的 $nin
(not in
) 比较运算符
$nin
是 mongodb 中的比较运算符之一。该运算符选择那些字段值不属于指定数组的文档,或者该字段不存在。
如果该字段包含一个数组、文档数组或嵌入文档数组,那么我们将仅获取该字段包含该数组且没有项目等于给定数组中的值的那些文档(我们稍后也会看到这种情况在本教程中)。
在深入了解更多细节之前,让我们创建包含一些文档的示例集合。
示例代码:
db.createcollection('students');
db.students.insertmany([
{
"name": {first: "mehvish", last: "ashiq"},
"age": 30,
"gender": "female",
"discipline": "bscs",
"joining_year": 2014,
"department": "computer science",
"courses":[ "python","java", "machine learning", "data science"],
"contact":[
{ phone: { type: "cell", number: "923042516485" }},
{ mail: { type: "official", email: "mehvishofficial@gmail.com"}}
]
},
{
"name": {first: "aftab", last: "raza"},
"age": 25,
"gender": "male",
"discipline": "bsit",
"joining_year": 2012,
"department": "information technology",
"courses":[ "python","javascript", "information security"],
"contact":[
{ phone: { type: "landline", number: "111-444-5555" }},
{ mail: { type: "personal", email: "aftab@hotmail.com"}}
]
}
])
db.students.find().pretty()
输出:
{
"_id" : objectid("6298ef54271c5124b739d7d3"),
"name" : {
"first" : "mehvish",
"last" : "ashiq"
},
"age" : 30,
"gender" : "female",
"discipline" : "bscs",
"joining_year" : 2014,
"department" : "computer science",
"courses" : [
"python",
"java",
"machine learning",
"data science"
],
"contact" : [
{
"phone" : {
"type" : "cell",
"number" : "923042516485"
}
},
{
"mail" : {
"type" : "official",
"email" : "mehvishofficial@gmail.com"
}
}
]
}
{
"_id" : objectid("6298ef54271c5124b739d7d4"),
"name" : {
"first" : "aftab",
"last" : "raza"
},
"age" : 25,
"gender" : "male",
"discipline" : "bsit",
"joining_year" : 2012,
"department" : "information technology",
"courses" : [
"python",
"javascript",
"information security"
],
"contact" : [
{
"phone" : {
"type" : "landline",
"number" : "111-444-5555"
}
},
{
"mail" : {
"type" : "personal",
"email" : "aftab@hotmail.com"
}
}
]
}
文档有点复杂的唯一原因是学习使用 $nin
比较运算符与不同的字段。例如,单个字段、包含嵌入文档的字段、包含数组的字段以及嵌入文档的数组。
在 mongodb 中使用 $nin
运算符和 find()
方法查询字段
示例代码:
db.students.find({ "joining_year": { $nin: [2011,2014] }}).pretty();
输出:
{
"_id" : objectid("6298ef54271c5124b739d7d4"),
"name" : {
"first" : "aftab",
"last" : "raza"
},
"age" : 25,
"gender" : "male",
"discipline" : "bsit",
"joining_year" : 2012,
"department" : "information technology",
"courses" : [
"python",
"javascript",
"information security"
],
"contact" : [
{
"phone" : {
"type" : "landline",
"number" : "111-444-5555"
}
},
{
"mail" : {
"type" : "personal",
"email" : "aftab@hotmail.com"
}
}
]
}
在这个例子中,我们使用 $nin
运算符和 find()
方法来搜索 joining_year
既不是 2011
也不是 2014
的整个文档。
如果我们只想拥有某些字段而不是整个文档,我们可以按以下方式使用该命令。写 1
以在计算机屏幕上打印该字段及其值,而 0
表示我们不希望该字段出现在结果集中。
示例代码:
db.students.find(
{ "joining_year": { $nin: [2011,2014] }},
{"name": 1, "discipline": 1, "department": 1, "_id":0}
).pretty();
输出:
{
"name" : {
"first" : "aftab",
"last" : "raza"
},
"discipline" : "bsit",
"department" : "information technology"
}
在 mongodb 中使用 $nin
运算符和 find()
方法查询嵌入式文档
示例代码:
db.students.find(
{ "name.last": { $nin: ["raza", "ali"] }},
{ "name": 1, "gender": 1, "age": 1, "_id":0}
).pretty();
输出:
{
"name" : {
"first" : "mehvish",
"last" : "ashiq"
},
"age" : 30,
"gender" : "female"
}
对于本文,示例文档有一个名为 name
的字段,其中还包含具有两个字段(first
和 last
)的嵌入文档。要将 $nin
比较运算符与 name
字段一起使用,我们使用点表示法 name.first
。
对于这个代码片段,我们试图检索那些 name.last
的值不是 $nin
的指定数组成员的文档的 name
、age
和 gender
运算符。
我们从那些 name.last
既不是 raza
也不是 ali
的文档中获取指定字段。我们还可以将 and
条件与 $nin
运算符一起使用。
示例代码:
db.students.find(
{ "name.last": { $nin: ["raza", "ali"]}, "name.first": {$nin: ["mehvish"]}},
{ "name": 1, "gender": 1, "age": 1, "_id":0}
).pretty();
这一次,我们不会得到任何输出,因为我们有两个文档,其中第一个文档的 mehvish
作为 name.first
的值,而第二个文档包含 raza
作为 name.last
字段的值.因此,这两个文档都被排除在结果集中,我们什么也没得到。
在 mongodb 中使用 $nin
运算符和 find()
方法中查询数组
示例代码:
db.students.find(
{ "courses": { $nin: ["javascript", "networking", "psychology"] }},
{ "courses": 1, "department": 1, "_id":0}
).pretty();
输出:
{
"department" : "computer science",
"courses" : [
"python",
"java",
"machine learning",
"data science"
]
}
仔细理解这个输出。在此输出中,course
字段包含一个数组,其中没有元素等于 $nin
运算符的给定数组中的值。
整个文档,其中包含元素等于 javascript
、networking
或 psychology
的数组的任何字段都将从结果集中排除。
在 mongodb 中使用 $nin
运算符和 find()
方法查询文档数组
仔细观察我们在本教程开始时填充 students
集合时使用的 contact
字段。它包含一个文档数组,其中每个文档都有一个嵌入(嵌套)文档。
如何使用 $nin
运算符进行查询?请参见下面给出的示例。
db.students.find(
{ "contact.phone.type": { $nin: ["cell"] }},
{ "contact": 1, "department": 1, "_id":0}
).pretty();
输出:
{
"department" : "information technology",
"contact" : [
{
"phone" : {
"type" : "landline",
"number" : "111-444-5555"
}
},
{
"mail" : {
"type" : "personal",
"email" : "aftab@hotmail.com"
}
}
]
}
使用 $nin
比较运算符和 update()
方法来更新 mongodb 中的字段值
示例代码:
db.students.update(
{ "joining_year": {$nin: [2011,2014]}},
{ $set: {"joining_year": 2000}}
);
在这个代码片段中,我们检索了 joining_year
既不是 2014
也不是 2011
的文档,然后将 joining_year
的值设置为 2000
。接下来,使用以下命令查看更新后的文档。
db.students.find().pretty()
在 mongodb 中使用带有正则表达式的 $nin
运算符
示例代码:
var array_of_regex = [/data /];
db.students.find(
{ "courses": {$nin: array_of_regex}},
{"name":1, "courses":1, "department":1, "_id":0}
).pretty();
输出:
{
"name" : {
"first" : "aftab",
"last" : "raza"
},
"department" : "information technology",
"courses" : [
"python",
"javascript",
"information security"
]
}
对于这个例子,我们创建了一个可以包含不同正则表达式的数组。目前,我们只有一个正则表达式。
为什么我们必须制作一组正则表达式?这是因为 $nin
需要一个数组来比较。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 mongodb shell 中列出所有数据库
发布时间:2023/05/11 浏览次数:180 分类:mongodb
-
交互式 mongo shell 提供了多个用于获取数据的选项。 本文介绍了在 mongo shell 中列出数据库的几种不同方法。
mongodb 中检查字段包含的字符串
发布时间:2023/05/11 浏览次数:1024 分类:mongodb
-
这篇文章解决了如何在 mongodb 中使用正则表达式来确定字段是否包含字符串。在 mongodb 中使用正则表达式 正则表达式 (regex) 是定义搜索模式的文本字符串。
在 mongodb 中 upsert 更新插入
发布时间:2023/05/11 浏览次数:214 分类:mongodb
-
在 mongodb 中,upsert 结合了更新和插入命令。 它可以在 update() 和 findandmodify() 操作中使用。mongodb 中的 upsert 查询 upsert 采用单个布尔参数。
如何卸载 mongodb
发布时间:2023/05/11 浏览次数:745 分类:mongodb
-
要从您的计算机中卸载 mongodb,您必须先删除 mongodb 服务、数据库和日志文件。使用这篇 mongodb 文章,您将能够从 ubuntu linux、mac 和 windows 卸载 mongodb。 请务必保留数据备份,因为一旦卸载,便
在 mongodb 中存储日期和时间
发布时间:2023/05/11 浏览次数:762 分类:mongodb
-
本 mongodb 教程解释了 date() 对象是什么以及如何使用 date() 方法对集合进行排序。 这也将帮助您找到在 mongodb 中显示和存储日期/时间的最佳方法。
mongodb 按 id 查找
发布时间:2023/05/11 浏览次数:1856 分类:mongodb
-
mongodb 中的 find by id() 函数用于获取与用户提供的 id 相匹配的文档。 如果找不到与指定 id 匹配的文档,则返回空值。
检查 mongodb 服务器是否正在运行
发布时间:2023/05/11 浏览次数:247 分类:mongodb
-
这篇 mongodb 教程将告诉您如何检查是否安装了 mongodb 以及安装的 mongodb 服务器的版本。 它在 windows、ubuntu 和 mac 等不同的操作系统中实现。
mongodb 中的分页
发布时间:2023/05/11 浏览次数:174 分类:mongodb
-
这篇文章将介绍什么是 mongodb 中的分页。 为什么在 mongodb 中需要分页以及在 mongodb 中完成分页的不同方法或方式是什么。
mongodb 从查询开始
发布时间:2023/05/11 浏览次数:186 分类:mongodb
-
在这篇 mongodb 文章中,用户将学习如何使用 $regex 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。