Home > OS >  mongodb filter where property not ends with
mongodb filter where property not ends with

Time:01-26

I have a mongodb collection with documents structured like this

{
   ...
   "ownerName":"Bob",   
   "city":"Oregon",
   "refNote":"sadkj1233233@qwertz",
}

I want to filter and select only documents where refNote not ends with @qwertz

CodePudding user response:

You can use regular expression and $not operator,

  • $regex to pass string that you want to search, specify $ at the end of string to search exact from end of the string,
  • $not to check opposit condition
db.collection.find({
  refNote: {
    $not: {
      $regex: "@qwertz$"
    }
  }
})

Playground

  •  Tags:  
  • Related