mongodb 项目嵌套字段
今天,我们将学习如何使用 $project
和 $unset
聚合阶段、foreach()
循环和 mapreduce()
方法在 mongodb 中查询数据时投影嵌套字段。
mongodb 项目嵌套字段
在 mongodb 中,我们可以使用 find()
方法检索所有文档,但如果我们只想访问特定的嵌套字段怎么办。这就是我们使用投影的地方。
我们可以通过各种方式投影嵌套字段。在这里,我们将了解以下项目嵌套字段的ag捕鱼王app官网的解决方案。
为了学习上述方法,让我们创建一个名为 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_id
和 city_name
并将它们保存在 project
对象中,同时使用括号表示法为 project
对象创建属性。此外,我们将 regions
字段保存在 project["regions"]
中。
接下来,我们有另一个名为 find
的对象,我们将在 aggregate()
方法中使用它来匹配文档。在 aggregate()
方法中,我们使用 $match
阶段来匹配文档,并使用 $project
来投影字段,无论是嵌套的还是第一级的。
我们使用 $project
来指定要在输出中显示的字段。如果我们只想在没有任何过滤查询的情况下投影指定的嵌套字段,我们可以使用以下ag捕鱼王app官网的解决方案。
示例代码:
// 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
运算符将匹配的元素替换为 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
集合的大小影响我们的操作。
我们可以通过使用新的无序 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 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 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。