swati kiran swati kiran - 4 months ago 39
Node.js Question

$nin with $and in mongoose

Filter a user(admin) using mongoose operations.

He need to see all saved files... except which as draft files..

But again he should be able to see his own saved draft file...


filter = {
$nin://not in (dont show)
// {_id : _id},//in this id and
{createBy: {$ne: userId}},//other dan admin and
{status: {$regex: /draft/, $options: 'm'}} //save as draft

I tried filtering this way but ended with this error..

exceptionMongoError: Can't canonicalize query: BadValue unknown top level operator: $nin

Thanks in advance...


You just need to find the docs that created by the user or those who are not draft:

files.find({$or: [{createBy: userId}, {status: /^((?!draft).)*$/}]})