$lookup no db.createView()

Asked

Viewed 94 times

0

I have a database of two Collections: companies and people.

And I want to create a view called "peopleSectors" from the collection people.

db.createView (
   "peopleSectors",
   "people",
   [
     { $lookup: { from: "companies", localField: "company_id", foreignField: "_id", as: "company_field" } },
     { $project: 
          { "_id": 0, 
            "first_name": 1, 
            "last_name": 1, 
            "job": 1,  
            "company": '$employer', 
            "sector": /* aqui mora o problema */
          }
     }
   ]
)

But within $project, when I create the schema of view, I want your field "sector" is equal to the sector field of Collection Companies within the $lookup.

How to access this collection? I know that to access Collection basis of view, just put the dollar in front, but and the other?

Schema example - "people"

{
    "_id" : ObjectId("57d7a121fa937f710a7d486e"),
    "last_name" : "Pham",
    "quote" : "Aliquam est reiciendis alias neque ad.",
    "job" : "Counselling psychologist",
    "ssn" : "401-31-6615",
    "address" : {
        "city" : "Burgessborough",
        "street" : "83248 Woods Extension",
        "zip" : "47201"
    },
    "first_name" : "Yvonne",
    "company_id" : ObjectId("57d7a121fa937f710a7d486d"),
    "employer" : "Terry and Sons",
    "birthday" : ISODate("2011-03-17T11:21:36Z"),
    "email" : "[email protected]"
}

Schema example - "Commissaries"

{
    "_id" : ObjectId("57d7a121fa937f710a7d486d"),
    "sector" : "Wholesale",
    "name" : "Terry and Sons",
    "mission" : "implement frictionless systems",
    "address" : {
        "city" : "Lake Meaganton",
        "state" : "Idaho",
        "street" : "211 Diane Shoals",
        "zip" : "10914-3394"
    },
    "logo" : "http://dummyimage.com/687x376"
}

1 answer

1


Each stage of the aggregation receives the result of the previous one. So just use it dot-Notation to access the desired field.

Try something like for example:

[
  {
    $lookup: {
      from: "companies",
      localField: "company_id",
      foreignField: "_id",
      as: "related_companies"
    }
  },
  {
    $project: {
      "_id": 0,
      "first_name": 1,
      "last_name": 1,
      "job": 1,
      "company": '$related_companies.name',
      "sector": '$related_companies.sector'
    }
  }
]
  • Perfect! I had tried to use the "$" in the name of the field as, but for some reason it hadn’t worked. Just one thing, I know it’s outside the scope of the question, but it’s the results of company and sector in view come out as arrays of a single item. How do I get strings loose?

  • 1

    You can add one more phase in the aggregation pipeline. {$unwind: "$related_companies"}. Thus, for each element of the array, a document will be returned. Since the field is an ID, I imagine there will only be one of each. https://docs.mongodb.com/v3.2/reference/operator/aggregation/unwind/

  • Thank you very much!! It worked... ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.