1

I have what should be a fairly simple query I'm trying to run in mongodb compass:

{ $and: [ { Source: "hostname" }, { Message: { $not: /.*unexpected data.*1234.*/ } } ] }

Basically, my document model contains a Source field and a Message field.

I need to query for all documents where the Source equals a given "hostname" and the Message does not contain "unexpected data...1234"

Everything works fine when I DO NOT include the $not filter on the regular expression... so I get all the documents where this message is contained... but now I need all the other messages where this is NOT contained... and I can't figure out how to use $not properly.

The example given in the MongoDb manual only shows using $not with one statement... but even this doesn't work for me for some reason...

{ Message: { $not: /.*unexpected data.*1234.*/ } }

Again, it works fine without the $not... what am I missing?

Edit:

Here is an image of what I'm talking about, placing this filter in MongoDb Compass, it indicates that the filter is incorrect... Is MongoDb Compass for some reason incapable of running complex filters? broken mongo filter

2 Answers 2

4

enter image description herePlease try with $regex which should work:

find( { $and: [ { Source: "hostname" }, { Message: { $not: { $regex: /.*unexpected data.*1234.*/ } } } ] })
Sign up to request clarification or add additional context in comments.

4 Comments

Strangely enough, I tried that first and it didn't work either... Is there something else that could be causing the $not filter to not function?
it works for me. Check out the screenshot that I've attached. Please post the error message you get
thanks for the example! I made an edit to my question and added the filter example... MongoDb Compass indicates this filter is incorrect in some way but does not say how... what application are you using to run your example, maybe I can try it there instead?
I use Robo 3T - 1.2. If it works, please accept the answer to get this to closure
0

MongoDB Compass only supports regex surrounded by single quotes, not forward slashes. So $regex: /.*unexpected data.*1234.*/ should be $regex: '.*unexpected data.*1234.*'.

https://jira.mongodb.org/browse/COMPASS-2993

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.