简体   繁体   中英

Using $not $in $regex in PyMongo

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM