Home > Net >  Mongoose where value in array
Mongoose where value in array

Time:06-09

I have the below code to return all blogs where the blog author ID is equal to the ID of the first administrator (orgAdmin) in an array:

let officialBlogs = await Blog.find().where('author.id').equals(help.orgAdmins[0]);

But I can't work out how to adapt this code so I can return blogs where the author matches ANY of the administrators in the array (i.e. orgAdmin[0], orgAdmin[1], orgAdmin[2] etc. Can anyone please advise?

CodePudding user response:

You can use $in operator to get things based on passed ids like this:

const ids = [];
help.orgAdmins.forEach(id => ids.push(id));

let officialBlogs = await Blog.find({author.id: { $in: ids }});
  • Related