mongodb - 有没有办法最小化 $graphlookup 输出?
问题描述
我试图从 MongoDB 中的数据集中获取竞争对手的层次结构,据我所知,我的代码可以工作,但由于文档的大小,输出太难阅读。
我已经研究过 $project 并试图以这种方式最小化查找的大小,但无法弄清楚。
db.companies.aggregate([
{$match: {tag_list: { $regex: /computer/, $options: 'i' }, competitions:{$ne:[]}}},
{ $graphLookup: { from: "companies", startWith:
"$competitions.competitor.name", connectFromField:"competitions.competitor.name",
connectToField: "name", as: "competition_heirachy"}},
{$limit:20},
{$project: {_id:0, name:1, tag_list:1, competitions:1, competition_heirachy:1}}
]).pretty()
输出:
{
"name" : "Prevail Resources",
"tag_list" : "field-services, break-fix-services, technical-fix, computer-repair, desktop-support, it-staffing, it-consulting, dell-repair, ibm-repair, sony-repair, lexmark-repair, pc-repair, laptop-repair, notebook-repair, printer-repair",
"competitions" : [
{
"competitor" : {
"name" : "Modis IT",
"permalink" : "modis-it"
}
},
{
"competitor" : {
"name" : "Matrix Resources",
"permalink" : "matrix-resources"
}
},
{
"competitor" : {
"name" : "Innostaff",
"permalink" : "innostaff"
}
}
],
"competition_heirachy" : [
{
"_id" : ObjectId("52cdef7e4bab8bd67529bbe0"),
"name" : "Matrix Resources",
"permalink" : "matrix-resources",
"crunchbase_url" : "http://www.crunchbase.com/company/matrix-resources",
"homepage_url" : "http://www.matrixresources.com",
"blog_url" : "",
"blog_feed_url" : "",
"twitter_username" : "matrixresources",
"category_code" : "enterprise",
"number_of_employees" : null,
"founded_year" : 1983,
"founded_month" : null,
"founded_day" : null,
"deadpooled_year" : null,
"deadpooled_month" : null,
"deadpooled_day" : null,
"deadpooled_url" : null,
"tag_list" : null
... and the rest of the "Matrix Resources" document continues
我只需要显示查询的 $project 列表中的字段,而不是整个文档。
解决方案
推荐阅读
- php - PHP-file_get_contents:无法在单个 if 语句中检查两个 file_get_contents
- jenkins - 如何从詹金斯管道获取所有参数(参数化构建)?
- android - 无边框颜色的圆角Textfiled
- pandas - 比较 2 列,如果发现相等则替换为 None
- sql - 如何计算 SQL Server 中一列中的单词数
- javascript - 为什么扫描仪在使用 javascript 处理条码扫描时返回额外的代码?
- arrays - 创建特定大小的线性间隔数组
- azure - 构建和发布 Azure 函数以包含 function.json 文件
- vuejs2 - 如何让 Vue v-for 列表渲染与 Laravel 中的 Blade 视图一起使用
- java - 从电话簿中的我的应用程序中多次显示一些联系人