1

I am trying to get a hierarchy of competitors from a dataset in MongoDB, my code works as far as I can tell but the output is too difficult to read due to the size of the documents.

I have looked into $project and trying to minimize the size of the lookup that way but can't figure it out.

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()

Output:

{
        "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

I need to only show the fields in the $project list of the query rather than the whole document.

Ashh
  • 44,693
  • 14
  • 105
  • 132
  • Can I use the `restrictSearchWithMatch` to do this? – Aaron Hayton Nov 01 '19 at 10:42
  • 1
    No it's not yet possible, you can vote on this bug: https://jira.mongodb.org/browse/SERVER-38560 maybe with more votes they move it up in the prio list... – HoefMeistert Jun 30 '20 at 06:08
  • This is a huge limitation for me. In the industry I work for there's a hierarchy of insurance agents and Im super limited because of not being able to just bring the ID for the sponsor, instead of a huge doc containing all the sponsor data. To give you an idea, an agent may have a downline of 3,000 agents, who have individual downlines too. It's the only reason we are sticking with SQL Server for the recursive queries. – Danielle Mar 14 '23 at 04:03

0 Answers0