Mongoose Aggregation Returning Empty Array

Viewed 32

I'm having some issues aggregating collections with mongoose and a little confused what I am doing wrong. For context here is how my schemas are setup with response examples:

Skill:

const SkillSchema: Schema = new Schema<Skill>({
  Skill: {
    type: String,
    required: true,
  },
  Date: {
    type: Date,
    required: true,
  },
});
{
  "_id": "627092699d087d0e7db4f359",
  "Skill": "AdSense Tracker",
  "Date": "2022-05-03T02:23:01.611Z"
}

UserSkill:

const UserSkillSchema: Schema = new Schema<UserSkill>({
  userID: {
    type: String,
    required: true,
  },
  skillID: {
    type: String,
    required: true,
  },
});
[
  {
    "_id": "62a39c6f2a73b0cc3af71e56",
    "userID": "625decf49b367400701f6f67",
    "skillID": "627092699d087d0e7db4f359"
  },
  {
    "_id": "62a39c6f2a73b0cc3af71e58",
    "userID": "625decf49b367400701f6f67",
    "skillID": "627092699d087d0e7db4f35c"
  },
  {
    "_id": "62a39c702a73b0cc3af71e5a",
    "userID": "625decf49b367400701f6f67",
    "skillID": "627092699d087d0e7db4f35d"
  }
]

I've been trying to create an aggregation between my userSkills collection and my skills collection so that I am able to get all the names of the skills a user has rather than just the skillID. I setup my function like this:

UserSkillModel.aggregate([
  {
    $lookup: {
      from: "skills",
      localField: "skillID",
      foreignField: "_id",
      as: "skill",
    },
  },
]).exec((err: CallbackError, userSkills: Skill[]) => {
  if (err) {
    res.status(500).send(err);
  }
  res.status(200).send(userSkills);
});

But for some reason, I am getting an empty array for skill which I expect to be the skill associated to the skillID from the skills collection. This is what my current response looks like:

[
  {
    "_id": "62a39c6f2a73b0cc3af71e56",
    "userID": "625decf49b367400701f6f67",
    "skillID": "627092699d087d0e7db4f359",
    "__v": 0,
    "skill": [
      
    ]
  },
  {
    "_id": "62a39c6f2a73b0cc3af71e58",
    "userID": "625decf49b367400701f6f67",
    "skillID": "627092699d087d0e7db4f35c",
    "__v": 0,
    "skill": [
      
    ]
  },
  {
    "_id": "62a39c702a73b0cc3af71e5a",
    "userID": "625decf49b367400701f6f67",
    "skillID": "627092699d087d0e7db4f35d",
    "__v": 0,
    "skill": [
      
    ]
  }
]

What am I doing wrong? I'm very confused here as I feel like this should be setup correctly but I'm assuming its a small mistake causing this problem. So, any help would be greatly appreciated.

Thanks in advance!

Edit

From the comment thread with R2D2 I was able to see that the code works perfectly fine in Mongo Playground but for whatever reason doesn't seem to work in my application. The only thing I can think of is that I am using the schema for the aggregate function but I can't figure out why this wouldn't be working.

1 Answers

It looks like you provide wrong collection name in the lookup , but the aggregation query is ok in general , check the playground

Related