Aggregate returning empty array

this is my code:


  const Followers = await followers.aggregate([
    { "$match": { "userId": userId }},
    {
      $project:{
        _id: 1,
        f_id: {"$toObjectId": "$followerId"}
      }
   },
    {
    $lookup:{
      from: 'users',
      localField: 'f_id', 
      foreignField: 'id',
      as: 'user'
    }
  }]);

Please share sample documents from both collections.

Without the data it is impossible to tell what is wrong with your pipeline.

Follower Model:


import mongoose from "mongoose";
import { stripVTControlCharacters } from "util";
const Schema = mongoose.Schema;

const followersSchema = new Schema(
    {
        followerId: {
            type: String,
            required: true,
            unique: false
        },
        userId: {
            type: String,
            required: true,
            unique: false
        }
    }
)

const followers = mongoose.models.followers || mongoose.model("followers", followersSchema);
export default followers;

Users Model:

import mongoose from "mongoose";
import { stripVTControlCharacters } from "util";
const Schema = mongoose.Schema;

const usersSchema = new Schema(
    {
        username: {
            type: String,
            required: true,
            unique: true
        },
        fullname: {
            type: String,
            required: false,
            unique: false
        },
        pronouns: {
            type: String,
            required: false,
            unique: false
        },
        email: {
            type: String,
            required: true,
            unique: true
        },
        instagramHandle: {
            type: String,
            required: false,
            unique: false
        },
        twitterHandle: {
            type: String,
            required: false,
            unique: false
        },
        link1: {
            type: String,
            required: false,
            unique: false
        },
        link2: {
            type: String,
            required: false,
            unique: false
        },
        bio: {
            type: String,
            required: false,
            unique: false
        },
        category: {
            type: [],
            required: false,
            unique: false
        },
        avatar: {
            type: String,
            required:false,
            unique:false
        },
        bannerImage: {
            type: String,
            required:false,
            unique:false
        },
        hashedPassword: {
            type: String,
            required: true,
            minlength: 5,
            unique:false
        },
        admin: {
            type: Boolean,
            required: false,
            unique:false
        },
        featured:{
            type: Boolean,
            required: false,
            unique:false
        },
        onboardingFormComplete:{
            type: Boolean,
            required: false,
            unique:false
        },
        location: {
            type: String,
            required: false,
            unique:false
        },
        dateJoined: {
            type: Date,
            required: false,
            unique:false
        }


    }
)

const users = mongoose.models.users || mongoose.model("users", usersSchema);
export default users;

I do not use mongoose, I do not use schema. I asked for

The schema does not provide us with real data to experiment with. Time is limited and most of us will not take the time to created our own documents from your schema. You certainly have documents that we can cut-n-paste directly into our system.

However what I can say is that you are using foreignField : id but your users schema does not specify a field named id.

user:

{"_id":{"$oid":"6423d23d8dc1c6ee2ef05ca7"},"username":"genwav","email":"genesisbarriosdev@gmail.com","category":["Art","Services"],"hashedPassword":"$2...","onboardingFormComplete":false,"dateJoined":{"$date":{"$numberLong":"1680069181027"}},"__v":{"$numberInt":"0"},"bio":"Software Engineer, Creative Coder, Artist, Producer","instagramHandle":"gen.wav","link1":"http://genesisbarrios.co","location":"Miami, FL","twitterHandle":"gendotwav","avatar":"data:image/jpeg;base64,/..."}

followers

{"_id":{"$oid":"6423ce8b8dc1c6ee2ef05b65"},"followerId":"6423d23d8dc1c6ee2ef05ca7","userId":"64062bdf13bc30624bddcdde","__v":{"$numberInt":"0"}}

Same conclusion:

But I can see that followerId matches the _id from the user. So simply replacing

with

      foreignField: '_id',

should work.

I strongly recommend that you store all your ids, such as userId and followerId as $oid because $oid takes less space than the string representation, $oid is faster to compare and you would be able to avoid the constant conversion you need to do for your $lookup.

1 Like

This line must be added in your schema.

.plugin(mongooseAggregatePaginate)