swati kiran swati kiran - 3 months ago 21
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...

Code:





filter = {
'_id':
$nin://not in (dont show)
[{
$and:
[
// {_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...

Answer

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).)*$/}]})