0

I want to do a PyMongo equivalent to vendor NOT IN ('Amazon', 'eBay', 'AliBaba').

I am able to get it to work in MongoDB by doing:

'vendor': {'$not': {'$in': [/^Amazon/, /^eBay/, /^AliBaba/]}}

This works.

In PyMongo no matter what I try, I am getting no result. It is not throwing any errors but not returning any results either.

Here is what I have tried:

1)

import re
vendor = {'$not': {'$in': [re.compile('^Amazon'), re.compile('^eBay'), re.compile('^AliBaba')]}}

2)

import re
vendor = {'$not': {'$in': [re.compile('.*Amazon.*'), re.compile('.*eBay.*'), re.compile('.*AliBaba.*')]}}

What am I missing? Why can't I get not in work with PyMongo?

nb_nb_nb
  • 1,243
  • 11
  • 36

1 Answers1

1

My guess is that maybe you are trying to design an expression that'd be somewhat close to:

^(?!.*\b(Amazon|eBay|AliBaba)\b).*$

not sure though.

Or maybe:

.*?\bAmazon\b.*
.*?\beBay\b.*
.*?\bAliBaba\b.*

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


and our code might look like:

db.collection.find({name:{'$regex' : '^(?!.*\b(Amazon|eBay|AliBaba)\b).*$', '$options' : 'i'}})

Reference

How can I use 'Not Like' operator in MongoDB

Emma
  • 27,428
  • 11
  • 44
  • 69