Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mongoose where value in array

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 }});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading