how to query mongodb using the $ne operator?
开发者_如何学Goi'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?
Try using $not instead of $ne.
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-Metaoperator%3A%24not
You can construct your query this way also
BasicDBObject query = new BasicDBObject();
query.append(fieldname,new BasicDBObject("$ne", value));
精彩评论