首页 > 解决方案 > 有没有办法最小化 $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 列表中的字段,而不是整个文档。

标签: mongodbgraphlookup

解决方案


推荐阅读