1

I have two collection

user

[{
    "id":1,
    "name":"a1",
    "emailAddress":"[email protected]",
},
{
    "id":2,
    "name":"a2",
    "emailAddress":"[email protected]",
},
{
    "id":3,
    "name":"a3",
    "emailAddress":"[email protected]",
}]

Organaziation

[{
    "emailAddress": "[email protected]",
    "name" : "org1"
},
{
    "emailAddress": "[email protected]",,
    "name" : "org1"
},
{
    "emailAddress" : "[email protected]",
    "name" : "org2"
}]

Now I want to fetch all users for organization org1 like below

[{
    "id":1, "name":"a1", "emailAddress":"[email protected]","orgName" : "org1"
},
{
    "id":2, "name":"a2", "emailAddress":"[email protected]","orgName" : "org1"
}]

I have checked debRef and lookup but those are returning in nested

How can I acheive this?

1
  • using mongoose or mongodb driver ? Commented Jul 6, 2018 at 8:53

1 Answer 1

2

You can simply achieve this using $lookup and $project aggregations

If you have mongodb version 3.6 and above

db.users.aggregate([
  { "$lookup": {
    "from": Organisation.collection.name,
    "let": { "emaildid": "$emaildid" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$emaildid", "$$emaildid" ] } } }
    ],
    "as": "organisation"
  }},
  { "$unwind": "$organisation" },
  { "$project": {
    { "id": 1, "name": 1, "emailid": 1, "org": "$organisation.org1" }
  }}
])

If you have mongodb version 3.4 and below

db.users.aggregate([
  { "$lookup": {
    "from": Organisation.collection.name,
    "localField": "emaildid",
    "foreignField": "emaildid",
    "as": "organisation"
  }},
  { "$unwind": "$organisation" },
  { "$project": {
    { "id": 1, "name": 1, "emailid": 1, "org": "$organisation.org1" }
  }}
])

try with $replaceRoot as well

db.Organisation.aggregate([
  { "$match": { "name": "org1" }},
  { "$lookup": {
    "from": Organisation.collection.name,
    "let": { "emailAddress": "$emailAddress", "name": "$name" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$emailAddress", "$$emailAddress" ] } } },
      { "$addFields": { "orgName": "$$name" }}
    ],
    "as": "users"
  }},
  { "$unwind": "$users" },
  { "$replaceRoot": { "newRoot": "$users" } }
])
Sign up to request clarification or add additional context in comments.

9 Comments

You need to aggregate with users collection and $lookup with organisation collection... See the updated answer... and make sure you put correct collection name inside $lookup i.e. in Organisation.collection.name
I manage to get data using ----- db.userorganizations.aggregate([ { $match: { "organization" : "test", }}, {$lookup: { from: "users", localField: "emailAddress", foreignField: "emailAddress", as: "userdetails"} }
Check the updated answer and please delete all the comments
unfortunately its not working, Could you please help me how can I get required fields from both collection in this query ----- b.userorganizations.aggregate([ { $match: { "organization" : "test", }}, {$lookup: { from: "users", localField: "emailAddress", foreignField: "emailAddress", as: "userdetails"} }---
Sorry not possible because I have put them into my collections and checked and get same output as you posted in your question... Please post the query in your question which you have tried
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.