MongoDB 中的 NOT IN 比较运算符

Mehvish Ashiq 2023年1月30日
  1. MongoDB 中的 $nin (NOT IN) 比较运算符
  2. 在 MongoDB 中使用 $nin 运算符和 find() 方法查询字段
  3. 在 MongoDB 中使用 $nin 运算符和 find() 方法查询嵌入式文档
  4. 在 MongoDB 中使用 $nin 运算符和 find() 方法中查询数组
  5. 在 MongoDB 中使用 $nin 运算符和 find() 方法查询文档数组
  6. 使用 $nin 比较运算符和 update() 方法来更新 MongoDB 中的字段值
  7. 在 MongoDB 中使用带有正则表达式的 $nin 运算符
MongoDB 中的 NOT IN 比较运算符

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

本教程介绍如何在 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 需要一个数组来比较。

作者: Mehvish Ashiq
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

相关文章 - MongoDB Operator