3

Here is my find request:

Recipe.find({'author.id': {$not: {user}}}

Here is the documentation: https://docs.mongodb.com/manual/reference/operator/query/not/index.html

Here is the error:

Error: Can't use $not with ObjectId.

I want to find all Recipes that are not authored by the current user. The arguments are working fine, so that's not the issue. There must be a way to do this, or am I doing something wrong?

David
  • 944
  • 1
  • 13
  • 20
  • I think you want `$ne` 'not equal' rather than `$not` which is a logical operator. see: https://docs.mongodb.com/manual/reference/operator/query/ne/ – Mark Nov 15 '17 at 01:34
  • I'm thinking you actually mean `.find({ "author.id": { "$ne": user } })` here. So it's `$ne` meaning "not equal" instead of `$not`, which is something else. And it's just `user` and not `{ user }` which actually means `{ "user": user }` and really does not make sense in combination with using "dot notation" already. – Neil Lunn Nov 15 '17 at 01:35

1 Answers1

13

You should use $ne

Recipe.find({ "author.id": { "$ne": user }});
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396