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?

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.