"$not" operator (and C++ driver)

47 views
Skip to first unread message

Szaniszlo Szöke

unread,
Apr 15, 2016, 10:13:04 AM4/15/16
to mongodb-user
Hi everyone,

I'm trying to get the opposite of a complex search using $not.
Let's take a very simple case.
Searching for:  (_id == 5) || (_id == 9) is done with command:
{ $or: [{_id: 5}, {_id: 9}]}

I expected to find the opposite by writing:
{$not: { $or: [{_id: 5}, {_id: 9}]}}

but that doesn't work, the correct syntax being:
{$and: [{_id: {$ne: 5}}, {_id: {$ne: 9}}]}

in other words: is there a way to reverse a query, like in SQL, by using a NOT command in front of it ?

Note: the real query I have to reverse is a mix of AND, OR, REGEX, etc. so I have no way to parse it and reverse it "by hand".

For those interested, the equivalent C++ code looks like:
                mongo::BSONObj obj1 = BSON("_id" << BSON("$eq" << 5));
                mongo::BSONObj obj2 = BSON("_id" << BSON("$eq" << 9));
                mongo::BSONObj obj12 = mongo::OR(obj1, obj2);

// unfortunately, this doesn't work:
                mongo::BSONObj obj = BSON("&not" << obj12);

Thanks in advance for any help.

Asya Kamsky

unread,
Apr 15, 2016, 10:38:20 AM4/15/16
to mongodb-user
Do you mean the $nor operator?


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/cf7b6523-8cf8-49fd-ad38-0ec84d4004e5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers

Szaniszlo Szöke

unread,
Apr 15, 2016, 10:42:18 AM4/15/16
to mongodb-user
No, I mean the $not operator

Szaniszlo Szöke

unread,
Apr 15, 2016, 10:46:05 AM4/15/16
to mongodb-user
In SQL, this query:
select _id, name from table_0 where (_id = 5) || (_id = 9) ;

becomes this query:
select _id, name from table_0 where not((_id = 5) || (_id = 9)) ;

how can I do that with MongoDB ?

Asya Kamsky

unread,
Apr 18, 2016, 12:10:08 AM4/18/16
to mongod...@googlegroups.com
And I'm  saying you want to use the $nor operator.  Read what it does. 


On Friday, April 15, 2016, Szaniszlo Szöke <szoke.s...@gmail.com> wrote:
No, I mean the $not operator

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/c985ef39-00ee-4714-b7d7-90da7c20ff73%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Szaniszlo Szöke

unread,
Apr 18, 2016, 3:14:50 AM4/18/16
to mongodb-user
Thanks a lot, it works fine.
Reply all
Reply to author
Forward
0 new messages