I am currently trying to find if an array of strings contains a certain string. So far, what I have is:
Following.find({ username: username }, { following: { $in: [profileUsername] } }).exec((err, result) => {
if (err) {
console.log(err);
res.json(err);
} else {
res.json(result);
}
});
However, it says that $in expects two arguments. Is there any better way to check whether the array contains the string? Thanks
2
Answers
$in
doesn’t receive 2 arguments, you just have a syntax error, the second objectfind
receives is query options, not a query. You want to rewrite your query like so:You don’t need to use the
$in
query filter as this is to match an item within a list of items, you can just do a normal equalityCheckout the mongo playground example: https://mongoplayground.net/p/cPF484_xqW5