I make an API with Express.js and mongoose. I need to find users whose id is contained in an array.
// the array of ids
const followedIds = follow.map((f) => f.followed);
console.log(followedIds);
// return [ '5ebaf673991fc602045ed47f', '5ebc6fb9af8add09edd842e9' ]
All IDs in this array exist.
Then I do :
User.where('_id').in(followedIds).exec()
.then((followedUser) => {
console.log('followedUser', followedUser);
// return []
});
followedUser
should have two users objects with the matching ids.
How can I resolve this?
Thanks
PS: There is my User model
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);