How to query a Array[String] for a regexp match? How to query a Array[String] for a regexp match? mongodb mongodb

How to query a Array[String] for a regexp match?


You can ignore the fact that this is an array:

> db.rx.insert( { "ips" : ["192.168.1.231", "192.168.2.231", "120.32.42.51"] });> db.rx.find( { ips : /192./ } ){ "_id" : ObjectId("4f104f0183bfca7a48b60da1"),   "ips" : [ "192.168.1.231", "192.168.2.231", "120.32.42.51" ] }

MongoDB always behaves like this: if you treat an array just like a normal field, it will apply the operation to each member and, if one matches, consider the parent document a match.