Joshua Dela Cruz
Joshua Dela Cruz

Reputation: 31

how to search multiple documents by giving multiple id's at the sametime in express and mongo db using mongoose

this is my route handler

router.get("/get-bookmarks", async (req, res) => {
  const bookmarks = await Post.find({ _id: { $all: req.body.idList } });
  if (!bookmarks) return res.status(404).send("No bookmarks yet");
  return res.send(bookmarks);
});

this is the data to search using postman

{
"idList": ["60c07302f033f51f6c87c986","60c07399f033f51f6c87c989","60c0736ff033f51f6c87c988"]
}

Upvotes: 1

Views: 543

Answers (1)

NeNaD
NeNaD

Reputation: 20304

You can do it with $in operator, like this:

Post.find({ _id: { $in: req.body.idList } });

Here is the working example: https://mongoplayground.net/p/gs-nuaB-_Ux

Upvotes: 1

Related Questions