Home > Blockchain >  populate collection is returning an empty array in mongoose using express
populate collection is returning an empty array in mongoose using express

Time:12-10

This is my how my Like model schema looks like.


//create likes schema
const likes = mongoose.Schema({
    liked: {
        type: Boolean,
        default: false
    },
    tweet: {
        type: Schema.Types.ObjectId,
        ref: "Tweet"
    },
    author: {
        type: Schema.Types.ObjectId,
        ref: "User"
    }
});


module.exports = mongoose.model('Like', likes);

and this is my an overview of my Tweet Schema:

const tweets = mongoose.Schema({
    content: {
        type: String,
        required: true,
    },
    author: {
        type: Schema.Types.ObjectId,
        ref: "User"
    },
    likes: [{
            type: Schema.Types.ObjectId,
            ref: "Like"
        }]
});


module.exports = mongoose.model('Tweet', tweets);

I am testing on based on the following data from

const likes = await Like.find().populate("author", "_id name email").populate("tweet", "_id content").exec()
res.json(likes)
[
    {
        "_id": "63921e53deb31c60249901e4",
        "liked": true,
        "tweet": {
            "_id": "63921e50deb31c60249901e1",
            "content": "tweet 1"
        },
        "author": {
            "_id": "63921e2ddeb31c60249901dd",
            "name": "Dave",
            "email": "[email protected]"
        },
        "createdAt": "2022-12-08T17:26:43.650Z",
        "updatedAt": "2022-12-08T17:26:43.650Z",
        "__v": 0
    }
]

And this is how I am using the populate method to fetch the likes of a tweet.

const tweets = await Tweet.find()
    .populate("author", "_id name email")
    .populate("likes", "_id")
    .sort({updatedAt: "desc"})
    .exec()

res.status(200).json(tweets)

but I am getting an empty array in likes collection (of objects).

[
    {
        "_id": "6393701aa62997f3454e81e1",
        "content": "My tweet",
        "author": "63936ffaa62997f3454e81dd",
        "likes": [],
        "createdAt": "2022-12-09T17:27:54.146Z",
        "updatedAt": "2022-12-09T17:27:54.146Z",
        "__v": 0
    }
]

Followed this documentation

this is the data from likes schema

[
    {
        "_id": "63937140df6222756bd84ede",
        "liked": true,
        "tweet": {
            "_id": "6393701aa62997f3454e81e1",
            "content": "My tweet"
        },
        "author": {
            "_id": "63936ffaa62997f3454e81dd",
            "name": "Dave",
            "email": "[email protected]"
        },
        "createdAt": "2022-12-09T17:32:48.251Z",
        "updatedAt": "2022-12-09T17:32:48.251Z",
        "__v": 0
    }
]

CodePudding user response:

As i understand, you want to populate nested objects

Mongoose populate syntax is:

populate({ path: 'refKey'}).

CodePudding user response:

I would suggest you use aggregate.

const tweets = await Tweet.aggregate([
    {
        $lookup: {
            from: "user",//your schema name in mongoose db
            localField: "author",//field name from Tweet which contains the id of user(auther)
            foreignField: "_id",//_id of user(auther) model
            pipeline: [
                {
                    $project: {
                        "_id": 1,
                        "name": 1,
                        "email": 1,
                    }
                }
            ],
            as: "author"
        }
    },
    {
        $lookup: {
            from: "likes",//your schema name in mongoose db
            localField: "likes",// or likes._id
            foreignField: "_id",
            pipeline: [
                {
                    $project: {
                        "_id": 1,
                    }
                }
            ],
            as: "likes"
        }
    },
    {
        $sort: {
            updatedAt: -1
        }
    }
])
  • Related