0

I have been trying to join two collections in MongoDB using the aggregate function but it seems it's not working for me, When I run API using lookup it shows me an empty collection [],

I have tried the following:

db.student.aggregate([
  {
    "$match": {
      _id: "63c4c245188267e988d690e2"
    },
    
  },
  {
    "$lookup": {
      "from": "wall",
      "localField": "_user",
      "foreignField": "_id",
      "as": "wallpost"
    }
  }
])

Result: Here is the result I m getting after lookup:

[
  {
    "_id": "63c4c245188267e988d690e2",
    "hereFor": [
      {
        "mainTag": "study",
        "subtag": [
          "studybuddy",
          "findtutor"
        ]
      }
    ],
    "lastName": "Kumar",
    "name": "Kamal",
    "profilePicture": [
      "https://airustudentimages.s3.ca-central-1.amazonaws.com/1673588594404-ba7777ef676f439f86aa612e8be67fd9"
    ],
    "wallpost": []
  }
]

Collections Collection I'm using in the query.

Student

student: [
    {
      "_id": "63c4c245188267e988d690e2",
      "name": "Kamal",
      "lastName": "Kumar",
      "profilePicture": [
        "https://airustudentimages.s3.ca-central-1.amazonaws.com/1673588594404-ba7777ef676f439f86aa612e8be67fd9"
      ],
      "hereFor": [
        {
          "mainTag": "study",
          "subtag": [
            "studybuddy",
            "findtutor"
          ]
        }
      ],
      
    },
    {
      "_id": "63c3965c201a1d738ab6867e",
      "name": "Suraksha",
      "lastName": "Singh",
      "profilePicture": [
        "https://airustudentimages.s3.ca-central-1.amazonaws.com/1673762449670-a8bdf9b9b0faf3ad84e0a5bc76e32fb8"
      ],
      "hereFor": [
        {
          "mainTag": "study",
          "subtag": [
            "studybuddy",
            "findtutor"
          ]
        }
      ],
      
    }
  ],

Wall

"wall": [
    {
      "_id": "63c4c92a188267e988d690e3",
      "_user": "63c3965c201a1d738ab6867e",
      "isSponsered": false,
      "description": "Hello test case ",
      "tag": {
        "mainTag": "hostels"
      },
      "createdAt": "1673766717308",
      "updatedAt": "1673766717308",
      
    },
    {
      "_id": "63c4cc2b188267e988d690e5",
      "_user": "63c3965c201a1d738ab6867e",
      "isSponsered": false,
      "description": "Hello test case 22 ",
      "tag": {
        "mainTag": "hostels"
      },
      "createdAt": "1673766717308",
      "updatedAt": "1673766717308",
      
    },
    
  ],

Have a look at https://mongoplayground.net/p/2moDXi3lygL

2 Answers 2

1

Your query is on Student collection. So the localField should be _id and and the foreignField should be _user (from wall collection).

Then it works fine.

db.student.aggregate([
  {
    "$match": {
      _id: "63c3965c201a1d738ab6867e"
    },
    
  },
  {
    "$lookup": {
      "from": "wall",
      "localField": "_id",
      "foreignField": "_user",
      "as": "wallpost"
    }
  }
])

https://mongoplayground.net/p/r1JeQIlM7AA

Sign up to request clarification or add additional context in comments.

Comments

1

You mix up the localField and foreignField.

From Equality Match with a Single Join Condition:

{
   $lookup:
     {
       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }
}

It should be:

{
  "$lookup": {
    "from": "wall",
    "localField": "_id",
    "foreignField": "_user",
    "as": "wallpost"
  }
}

Demo @ Mongo Playgound

Comments

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.