迹忆客 专注技术分享

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

MongoDB 项目嵌套字段

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

今天,我们将学习如何使用 $project$unset 聚合阶段、forEach() 循环和 mapReduce() 方法在 MongoDB 中查询数据时投影嵌套字段。

MongoDB 项目嵌套字段

在 MongoDB 中,我们可以使用 find() 方法检索所有文档,但如果我们只想访问特定的嵌套字段怎么办。这就是我们使用投影的地方。

我们可以通过各种方式投影嵌套字段。在这里,我们将了解以下项目嵌套字段的解决方案。

为了学习上述方法,让我们创建一个名为 nested 的集合,其中包含一个文档。你也可以使用下面给出的查询与我们联系。

示例代码:

// MongoDB version 5.0.8

> db.nested.insertOne(
    {
        "name": {
            "first_name": "Mehvish",
            "last_name": "Ashiq",
         },
         "contact": {
            "phone":{"type": "manager", "number": "123456"},
            "email":{ "type": "office", "mail": "delfstack@example.com"}
         },
         "country_name" : "Australien",
         "posting_locations" : [
             {
                 "city_id" : 19398,
                 "city_name" : "Bondi Beach (Sydney)"
             },
             {
                  "city_id" : 31101,
                  "city_name" : "Rushcutters Bay (Sydney)"
             },
             {
                  "city_id" : 31022,
                  "city_name" : "Wolly Creek (Sydney)"
             }
          ],
          "regions" : {
              "region_id" : 796,
              "region_name" : "Australien: New South Wales (Sydney)"
          }
    }
);

使用 db.nested.find().pretty(); 在 mongo shell 上查看插入的数据。

使用 $project 聚合阶段在 MongoDB 中投影嵌套字段

示例代码:

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> var project = {};
> project["id"] = "$"+current_location+".city_id";
> project["name"] = "$"+current_location+".city_name";
> project["regions"] = 1;

> var find = {};
> find[current_location] = {"$exists":true};

> db.nested.aggregate([
    { $match : find },
    { $project : project }
]).pretty()

输出:

{
        "_id" : ObjectId("62a96d397c7e3688aea26d0d"),
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "id" : [
                19398,
                31101,
                31022
        ],
        "name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}

在这里,我们将名为 posting_locations 的第一级字段保存在名为 current_location 的变量中。

然后,我们使用该变量访问 city_idcity_name 并将它们保存在 project 对象中,同时使用括号表示法为 project 对象创建属性。此外,我们将 regions 字段保存在 project["regions"] 中。

接下来,我们有另一个名为 find 的对象,我们将在 aggregate() 方法中使用它来匹配文档。在 aggregate() 方法中,我们使用 $match 阶段来匹配文档,并使用 $project 来投影字段,无论是嵌套的还是第一级的。

我们使用 $project 来指定要在输出中显示的字段。如果我们只想在没有任何过滤查询的情况下投影指定的嵌套字段,我们可以使用以下解决方案。

示例代码:

// MongoDB version 5.0.8

> var current_location = "posting_locations";
> db.nested.aggregate({
    $project: {
         "_id": 0,
         "city_id": "$" + current_location + ".city_id",
         "city_name": "$" + current_location + ".city_name",
         "regions": 1
    }
}).pretty();

输出:

{
        "regions" : {
                "region_id" : 796,
                "region_name" : "Australien: New South Wales (Sydney)"
        },
        "city_id" : [
                19398,
                31101,
                31022
        ],
        "city_name" : [
                "Bondi Beach (Sydney)",
                "Rushcutters Bay (Sydney)",
                "Wolly Creek (Sydney)"
        ]
}

使用 $unset 聚合阶段获取 MongoDB 中排除指定字段的嵌套字段

示例代码:

// MongoDB version 5.0.8

> db.nested.aggregate({
        $unset: ["posting_locations.city_id", "contact", "regions", "name", "_id"]
}).pretty()

输出:

{
        "country_name" : "Australien",
        "posting_locations" : [
                {
                        "city_name" : "Bondi Beach (Sydney)"
                },
                {
                        "city_name": "Rushcutters Bay (Sydney)"
                },
                {
                        "city_name": "Wolly Creek (Sydney)"
                }
        ]
}

在这里,我们使用 $unset 运算符,用于删除指定的字段或字段数组。

请记住,我们使用点符号来指定嵌入的文档或文档数组。如果给定字段不存在,$unset 运算符不执行任何操作。

当我们使用 $ 匹配数组的元素时,$unset 运算符将匹配的元素替换为 null 而不是从数组中删除它们。此行为有助于保持元素位置和数组大小一致。

使用 forEach() 循环在 MongoDB 中获取嵌套字段

示例代码:

// MongoDB version 5.0.8

> var bulk = db.newcollection.initializeUnorderedBulkOp(),
   counter = 0;

> db.nested.find().forEach(function(doc) {
    var document = {};
    document["name"] = doc.name.first_name + " " + doc.name.last_name;
    document["phone"] = doc.contact.phone.number;
    document["mail"] = doc.contact.email.mail;
    bulk.insert(document);
    counter++;
    if (counter % 1000 == 0) {
        bulk.execute();
        bulk = db.newcollection.initializeUnorderedBulkOp();
    }
});

> if (counter % 1000 != 0) { bulk.execute(); }

你将看到类似于以下内容的内容。

BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 1,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

接下来,在你的 mongo shell 上执行以下命令以查看投影字段。

// MongoDB version 5.0.8

> db.newcollection.find().pretty();

输出:

{
        "_id" : ObjectId("62a96f2d7c7e3688aea26d0e"),
        "name" : "Mehvish Ashiq",
        "phone" : "123456",
        "mail" : "delfstack@example.com"
}

为了学习这个示例代码,假设我们想要获取某些嵌套字段并将它们插入到一个新集合中。在这里,将转换后的字段作为文档插入到新集合中可能会根据 nested 集合的大小影响我们的操作。

我们可以通过使用新的无序 bulk insert API 来避免这种缓慢的插入性能。它将通过批量发送来简化插入操作,并实时向我们反馈操作是成功还是失败。

因此,我们使用 bulk insert API 将所需的数据结构插入 newcollection 集合中,其中全新的文档将使用 nested 集合游标的 forEach() 循环创建。要创建新属性,我们使用括号表示法。

对于此代码,我们假设有大量数据。因此,我们将把操作以 1000 的批次发送到服务器以执行批量插入操作。

结果,它为我们提供了良好的性能,因为我们不是向服务器发送每个请求,而是每 1000 个请求发送一次。

使用 mapReduce() 方法在 MongoDB 中投影嵌套字段

示例代码:

// MongoDB version 5.0.8

> function map() {
    for(var i in this.posting_locations) {
         emit({
             "country_id" : this.country_id,
             "city_id" : this.posting_locations[i].city_id,
             "region_id" : this.regions.region_id
         },1);
    }
}

> function reduce(id,docs) {
      return Array.sum(docs);
}

> db.nested.mapReduce(map,reduce,{ out : "map_reduce_output" } )

现在,运行以下查询以查看输出。

// MongoDB version 5.0.8
> db.map_reduce_output.find().pretty();

输出:

{
        "_id" : {
                "country_id" : undefined,
                "city_id" : 19398,
                "region_id" : 796
        },
        "value" : 1
}
{
        "_id" : {
                "country_id" : undefined,
                "city_id" : 31022,
                "region_id" : 796
        },
        "value" : 1
}
{
        "_id" : {
                "country_id" : undefined,
                "city_id" : 31101,
                "region_id" : 796
        },
        "value" : 1
}

对于这个示例代码,我们使用 mapReduce() 函数对 nested 集合的所有文档执行 map-reduce。为此,我们必须遵循下面简要说明的三步过程。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

比较 MongoDB 中的字段

发布时间:2023/04/21 浏览次数:51 分类:MongoDB

在本文中,我们将了解如何比较 MongoDB 中的两个字段。 此外,我们将看到一个相关的示例和解释,以使主题更容易理解。

清除或删除 MongoDB 中的集合

发布时间:2023/04/21 浏览次数:147 分类:MongoDB

本篇文章将告诉大家如何删除 MongoDB 数据库中的集合以及删除 MongoDB 中的集合的不同方法。

MongoDB 截断集合

发布时间:2023/04/21 浏览次数:178 分类:MongoDB

可以根据需要选择两个选项之一来截断下面的集合。 在今天的文章中,我们将学习如何在 MongoDB 中截断集合。

删除 MongoDB 中的重复项

发布时间:2023/04/21 浏览次数:151 分类:MongoDB

在本文中,我们将了解如何删除 MongoDB 中的重复条目,并且我们还将看到一个带有适当解释的示例,以使主题更容易理解。

使用 NodeJS 检查 MongoDB 中是否存在集合

发布时间:2023/04/21 浏览次数:194 分类:MongoDB

在本文中,我们将检查 MongoDB 数据库中是否存在一个集合,并且我们还将查看与主题相关的示例,以使主题更容易理解。 为此,我们将使用 Node.js。

MongoDB 中的唯一索引

发布时间:2023/04/21 浏览次数:144 分类:MongoDB

在这篇教学文章中,您将了解唯一索引、它们是什么以及如何在 MongoDB 中使索引唯一。 此外,还简要详细地解释了使用户的电子邮件在 MongoDB 中唯一。

在 MongoDB 中创建索引

发布时间:2023/04/21 浏览次数:104 分类:MongoDB

索引有助于有效解决查询。 如果没有索引,MongoDB 必须遍历集合中的每个文档才能找到与查询匹配的文档。因此,在今天的文章中,我们将学习如何在 MongoDB 中创建索引。

MongoDB 中的稀疏索引

发布时间:2023/04/21 浏览次数:142 分类:MongoDB

在本文中,我们将讨论 MongoDB 中的稀疏索引。 此外,我们将提供一个相关示例并进行解释,以使该主题更容易理解。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便