I'm using mongoose and I have the following list:
const ids: ["63bd7878f1f085f7d8a6827f", "63be730bf1f085f7d8a682c8"];
I want to turn ids
into a list of ObjectIds..
I've tried the following:
const affectedUsers = await Users.find(
{ _id: { $in: [ids.map(e => mongoose.Types.ObjectId(e))] } }
);
But it gives me the following error:
"message": "Cast to ObjectId failed for value \"[\n new
ObjectId(\"63bd7878f1f085f7d8a6827f\"),\n new
ObjectId(\"63be730bf1f085f7d8a682c8\")\n]\"
(type Array) at path \"_id\" for model \"users\""
You have added extra brackets at $in
query, try this...
const affectedUsers = await Users.find(
{ _id: { $in: ids.map(e => mongoose.Types.ObjectId(e)) } }
);