I have created an API using Express.js and mongoose to find users based on their ids in an array.
// Here is the array of user ids
const followedIds = follow.map((f) => f.followed);
console.log(followedIds);
// This will log [ '5ebaf673991fc602045ed47f', '5ebc6fb9af8add09edd842e9' ]
All the IDs in this array are valid and exist.
Next, I execute:
User.where('_id').in(followedIds).exec()
.then((followedUser) => {
console.log('followedUser', followedUser);
// This should ideally return two user objects that match the provided ids
});
The followedUser
variable should contain information about the specified users.
Could someone help me understand why my query is not returning the expected results?
Thank you!
PS: Below is my User model definition
const mongoose = require('mongoose');
const user = new mongoose.Schema({
username: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
avatar: { type: String, default: '/images/avatar_default.jpg' },
banner: { type: String, default: '/images/banner_default.jpg' },
created_at: { type: Date, required: true },
deleted_at: { type: Date, required: false },
}, { versionKey: false });
module.exports = mongoose.model('user', user);