使用 foreach() 更新 mongodb shell 中的数组字段
今天,我们将学习如何在使用 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}
]
为此,我们可以使用嵌套的 ,如下所示。
示例代码:
> 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()
用于迭代 collection
中 fields.items
字段的值。我们使用每个值来形成所需的更新并将其保存到 anewitem
变量中,该变量使用 push()
方法进一步插入 newitems
数组中。
之后,我们使用 onefield._id
和 newitems
数组创建一个文档,将其推入 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 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。