user1765862
user1765862

Reputation: 14145

mongodb filter where property not ends with

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

Upvotes: 2

Views: 1034

Answers (1)

turivishal
turivishal

Reputation: 36104

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

Upvotes: 4

Related Questions