MongoDB Regex Query : Why doesn't this work?

后端 未结 2 1364
[愿得一人]
[愿得一人] 2021-01-20 11:06

See second to last input please.

Note: I was using http://try.mongodb.org/

> person = {fullname : \"Derek Litz\"}
{
     \"fullname\" : \"Derek Li         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-20 11:59

    I think that's just a bug in try.mongodb.org. These work for me in my local mongo shell:

    db.people.find({first_name: {$regex: /e/}})
    db.people.find({first_name: /e/})
    

    And the documentation says this:

    You may use regexes in database query expressions:

    db.customers.find( { name : /acme.*corp/i } );
    db.customers.find( { name : { $regex : 'acme.*corp', $options: 'i' } } );
    [...]
    db.customers.find( { name : { $regex : /acme.*corp/i, $nin : ['acmeblahcorp'] } } );

    So both string and RegExp literal versions are supported.

提交回复
热议问题