Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to query mongodb using the $ne operator?

Tags:

java

mongodb

i'm trying to do a $ne query in mongodb while using a regex, but it doesn't seem to work. the $ne (not equal) operator works fine when i don't use a regex though.

BasicDBObject q = new BasicDBObject()
q.put(field, ["\$ne": value])

the above works fine, the result set doesn't contain any documents that has that value for that field.

but i need it to be case insensitive. so i did this

q.put(field, ["\$ne": Pattern.compile(value, Pattern.CASE_INSENSITIVE)])

but this doesn't work..

so i thought, let me go to the command line and see if i can do it manually. so i did this:

db.Order.find({"bill.recipient.name": {$ne: /diep/i}},{"bill.recipient.name":1})

and it still doesn't work!

any ideas?

like image 958
Khon Lieu Avatar asked Apr 10 '11 15:04

Khon Lieu


2 Answers

Try using $not instead of $ne.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-Metaoperator%3A%24not

like image 107
jazgot Avatar answered Sep 29 '22 02:09

jazgot


You can construct your query this way also

BasicDBObject query = new BasicDBObject();
    query.append(fieldname,new BasicDBObject("$ne", value));
like image 43
hiccup Avatar answered Sep 29 '22 00:09

hiccup