Reputation: 8481
The first query below shows the full content of the collection and the following three queries return one item as expected.
Why does the last query return two items?
> db.log.find()
{ "_id" : 123, "user" : "stefano" }
{ "_id" : 456, "user" : "franco" }
> db.log.find({'user':/st/})
{ "_id" : 123, "user" : "stefano" }
> db.log.find({'user':/fr/})
{ "_id" : 456, "user" : "franco" }
> db.log.find({'user':/st*/})
{ "_id" : 123, "user" : "stefano" }
> db.log.find({'user':/fr*/})
{ "_id" : 123, "user" : "stefano" }
{ "_id" : 456, "user" : "franco" }
Upvotes: 0
Views: 83
Reputation: 2667
Because in Regex the *
is a quantifier (matches between zero and unlimited times). So the /fr*/
regex matches f
literally and a r
zero or unlimited times. (https://regex101.com/r/M8h249/1)
If you would like to create a regex which matches like a "starts with" then you can use the following: /^fr/
.
Upvotes: 2