1

I am very new to Mongo engine and currently I have mongo query filter that is working in mongo shell but I wanted to achieve the same in Mongoengine , can you please help me

Mongo Shell Query {name:{$nin:[/ja/,/ra/]}}

but same is not working with Mongoengine

namelist = NameRecord.objects(name__nin=['/ja/','/ra/'])

Thanks in advance

2 Answers 2

2

First of all I would like to thank @tom-slabbaert for pointing me the what was wrong , as he pointed the issue was with regex of python and finally with his input I am able to find the solution, in fact all credits goes to him just I am adding answer so it will be helpful any others in future

namelist = NameRecord.objects(name__nin=[re.compile(r"ja"), re.compile(r"ra")])

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

Comments

1

In the Mongo shell you're using javascript regex syntax, /string/ is a regex and not a string. unlike in python where youse using these values are strings.

All you need to do is convert them to the python regex equivalent:

namelist = NameRecord.objects(name__nin=[r"ja", r"ra"])

7 Comments

I tried this but for some reason I am getting the expected result :(
Maybe you can edit in what your getting + the expect result?
Actually I was expecting to return the records only where name field doesn't contain ja or ra but this is resulting me even with names ja or ra
Thanks @Tom I have solved the issue with your inputs :)
Glad i could help.
|

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.