迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 数据库 > MongoDB >

MongoDB 中的 NOT IN 比较运算符

作者:迹忆客 最近更新:2023/05/09 浏览次数:

比较运算符在处理大型数据集时非常有用。从数据中获得洞察力是有帮助的。

本文介绍如何在 MongoDB 中使用 $nin (NOT IN) 比较运算符。

它还展示了如何在 MongoDB 中将 $ninfind()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 的字段,其中还包含具有两个字段(firstlast)的嵌入文档。要将 $nin 比较运算符与 name 字段一起使用,我们使用点表示法 name.first

对于这个代码片段,我们试图检索那些 name.last 的值不是 $nin 的指定数组成员的文档的 nameagegender 运算符。

我们从那些 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 运算符的给定数组中的值。

整个文档,其中包含元素等于 JavaScriptNetworkingPsychology 的数组的任何字段都将从结果集中排除。

在 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 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便