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
CodePudding user response:
$in
doesn't receive 2 arguments, you just have a syntax error, the second object find
receives is query options, not a query. You want to rewrite your query like so:
Following.find({ username: username, following: { $in: [profileUsername] } }).exec((err, result) => {
if (err) {
console.log(err);
res.json(err);
} else {
res.json(result);
}
});
CodePudding user response:
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 equality
Following.find({ username: username, following: profileUsername } })
Checkout the mongo playground example: https://mongoplayground.net/p/cPF484_xqW5