迹忆客 专注技术分享

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

使用 forEach() 更新 MongoDB Shell 中的数组字段

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

今天,我们将学习如何在使用 MongoDB shell 时使用 forEach() 来更新数组字段。

使用 forEach() 更新 MongoDB Shell 中的数组字段

要使用 forEach(),让我们准备一个名为 collection 的样本集合,其中包含两个文档。你也可以使用下面的查询来继续我们的工作。

示例代码:

> use updateArrayField
> db.createCollection('collection');
> db.collection.insertMany([
    {
          "name": "Mehvish",
          "gender": "Female",
          "fields" : [
              { "_id" : 1, "items" : [ 1,3,4,5,6,7 ] }
           ]
    },
    {
          "name": "Thomas Christopher",
          "gender": "Male",
          "fields" : [
              { "_id" : 1, "items" : [ 1,3,4,5,6,7 ] }
           ]
    }
]);

接下来,我们使用这些文档来更新名为 fields.items 的数组字段

"items" : [ 1,3,4,5,6,7]

"items" : [
   {item: 1, key: 0},
   {item: 3, key: 0},
   {item: 4, key: 0},
   {item: 5, key: 0},
   {item: 6, key: 0},
   {item: 7, key: 0}
]

为此,我们可以使用嵌套的 forEach(),如下所示。

示例代码:

> var table = db.collection.find();

> table.forEach(function( oneRow ) {
    var newFields = [];

    oneRow.fields.forEach( function( oneField ){
        var newItems = [];

        oneField.items.forEach( function( item ){
            var aNewItem = { item: parseInt(item), key: 0 };
            newItems.push( aNewItem );
        } );

        newFields.push({ _id: oneField._id, items: newItems });
    } )
    db.collection.update(
        { _id: oneRow._id },
        { "$set": { "fields": newFields } }
    );
});

在 mongo shell 上执行以下查询以查看更新的文档。

示例代码:

> db.collection.find()

输出:

{
    "_id" : ObjectId("62a708054eb9c63e48daeba4"),
    "name" : "Mehvish",
    "gender" : "Female",
    "fields" : [
        {
            "_id" : 1,
            "items" : [
                { "item" : 1, "key" : 0 },
                { "item" : 3, "key" : 0 },
                { "item" : 4, "key" : 0 },
                { "item" : 5, "key" : 0 },
                { "item" : 6, "key" : 0 },
                { "item" : 7, "key" : 0 }
            ]
        }
    ]
}
{
    "_id" : ObjectId("62a708054eb9c63e48daeba5"),
    "name" : "Thomas Christopher",
    "gender" : "Male",
    "fields" : [
        {
            "_id" : 1,
            "items" : [
                { "item" : 1, "key" : 0 },
                { "item" : 3, "key" : 0 },
                { "item" : 4, "key" : 0 },
                { "item" : 5, "key" : 0 },
                { "item" : 6, "key" : 0 },
                { "item" : 7, "key" : 0 }
            ]
       }
   ]
}

为了获得上述输出,我们从 collection 中读取所有数据并使用 forEach() 迭代 collection 的每个文档。接下来,我们使用另一个 forEach() 来遍历指定文档的每个字段。

然后,第三个 forEach() 用于迭代 collectionfields.items 字段的值。我们使用每个值来形成所需的更新并将其保存到 aNewItem 变量中,该变量使用 push() 方法进一步插入 newItems 数组中。

之后,我们使用 oneField._idnewItems 数组创建一个文档,将其推入 newFields 数组,该数组进一步用于更新 collection

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便