How to get negation of a MongoDB query? -
my query is:
db.users.find({"username" : {$regex : ".*substring.*"}});
instead of getting usernames contain 'substring', need usernames not contain it.
for this, can utilize $not operator:
db.test.find( { username: { $not: /substring/ } } );
like in:
> db.test.insert( { username: "derick rethans" } ); > db.test.insert( { username: "hannes magunusson" } ); > db.test.find( { username: { $not: /ag/ } } ); { "_id" : objectid("511258b36879ad400c26084f"), "username" : "derick rethans" }
however, need aware regular expressions and utilize of $not operator prevent indexes beingness able used. if need query often, might want have @ schema design.
mongodb
No comments:
Post a Comment