db.users.insert({name: 'paulo'})
db.users.insert({name: 'patric'})
db.users.insert({name: 'pedro'})
db.users.find({name: /a/}) //like '%a%'
out: paulo, patric
db.users.find({name: /^pa/}) //like 'pa%'
out: paulo, patric
db.users.find({name: /ro$/}) //like '%ro'
out: pedro
db.users.insert({name: 'patric'})
db.users.insert({name: 'pedro'})
db.users.find({name: /a/}) //like '%a%'
out: paulo, patric
db.users.find({name: /^pa/}) //like 'pa%'
out: paulo, patric
db.users.find({name: /ro$/}) //like '%ro'
out: pedro
Multiline Match for Lines Starting with Specified Pattern
The following example uses the m option to match lines starting with the letter S for multiline strings:
db.products.find( { description: { $regex: /^S/, $options: 'm' } } )
The query matches the following documents:
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
Without the m option, the query would match just the following document:
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
留言