mongo 正则及条件查询

  • $regex用法 官方地址
    举个例子来说:现在有以下集合(官网的例子):
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before     line" }
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }

db.collection.find( { sku: /abC/i } );等效于下面这种写法 
db.collection.find( { sku: { $regex: 'abC', $options: 'i' } } ); 
以上是个简单的应用。

参数介绍: 
Option ===== Description 
参数 i ====== 加了这个参数,表示不区分大小写

参数 m ===== 个人理解这个参数是用来匹配value中有换行符(\n)的情形。 
还有一个情形是:匹配规则中使用了锚,所谓的锚就是^ 开头, $ 结束 
比如:db.products.find( { description: { $regex: /^S/, $options: 'm' } } ) 
上面匹配规则的意思就是匹配description字段的value值中,以大写S开头的value值。 
匹配后结果是:

{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }

可以看出,第二条记录中descriptio的值包含\n换行字符,而他之所以能匹配出来就是因为 
添加了m 参数。 
假设没有添加m参数,语句就是

db.products.find( { description: { $regex: /^S/} }

此时匹配结果为

{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }

再比如我们连锚都不写也就是去掉^符合,语句变成

db.products.find( { description: { $regex: /S/ } } )

此时结果为:

{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }


此时可以分析出m参数的使用场景: 
应该是为了匹配字段value值中以某个字符开头(^),或者是某个字符结束($).即便value中包含换行符(\n)也能匹配到。 
从上例最后例子看出,m参数应该是和锚同时使用才有意思,否则直接去匹配也能匹配出来。说明m是在特殊需求下才使用的!

参数 s ===== 允许点字符(.)匹配所有的字符,包括换行符。

比如语句:

db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )

匹配value中包含m且之后为任意字符包括换行符并且还包含line字符的字符串。不区分大小写 
结果为:

{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before     line" }
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }


如果不加s参数时,语句为:

db.products.find( { description: { $regex: /m.*line/, $options: 'i' } } )

结果为:

{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before     line" }

参数 x ====== 官网的大意是忽视空白字符
允许在查询中使用聚合操作
官方例子1:

{ "_id" : 1, "category" : "food", "budget": 400, "spent": 450 }
{ "_id" : 2, "category" : "drinks", "budget": 100, "spent": 150 }
{ "_id" : 3, "category" : "clothes", "budget": 100, "spent": 50 }
{ "_id" : 4, "category" : "misc", "budget": 500, "spent": 300 }
{ "_id" : 5, "category" : "travel", "budget": 200, "spent": 650 }

db.monthlyBudget.find( { $expr: { $gt: [ "$spent" , "$budget" ] } } )
查询结果为:
{ "_id" : 1, "category" : "food", "budget" : 400, "spent" : 450 }
{ "_id" : 2, "category" : "drinks", "budget" : 100, "spent" : 150 }
{ "_id" : 5, "category" : "travel", "budget" : 200, "spent" : 650 }
找出spent大于budget的所有值

官方例子2:
{ "_id" : 1, "item" : "binder", "qty": 100 , "price": 12 }
{ "_id" : 2, "item" : "notebook", "qty": 200 , "price": 8 }
{ "_id" : 3, "item" : "pencil", "qty": 50 , "price": 6 }
{ "_id" : 4, "item" : "eraser", "qty": 150 , "price": 3 }

db.supplies.find( {
    $expr: {
       $lt:[ {
          $cond: {
             if: { $gte: ["$qty", 100] },
             then: { $divide: ["$price", 2] },
             else: { $divide: ["$price", 4] }
           }
       },
       5 ] }
} )

查询结果为:
{ "_id" : 2, "item" : "notebook", "qty": 200 , "price": 8 }
{ "_id" : 3, "item" : "pencil", "qty": 50 , "price": 6 }
{ "_id" : 4, "item" : "eraser", "qty": 150 , "price": 3 }

官方例子:
{ "_id" : 1, "item" : "abc123", "qty" : 0 }
{ "_id" : 2, "item" : "xyz123", "qty" : 5 }
{ "_id" : 3, "item" : "ijk123", "qty" : 12 }

db.inventory.find( { qty: { $mod: [ 4, 0 ] } } )
查询结果(找出除以4余数为0):
{ "_id" : 1, "item" : "abc123", "qty" : 0 }
{ "_id" : 3, "item" : "ijk123", "qty" : 12 }

$mod 数组的参数为2个,空、少或者多都会报错

  • $text
MongoDB支持对文本内容执行文本搜索操作,其提供了索引text index和查询操作$text来完成文本搜索功能。下面我们通过一个简单的例子来体验一下MongoDB提供的全文检索功能。

1.新建blogs collection,并插入如下的document。

db.blogs.insert({_id:1,title:"MongoDB text search",content:"this is a simple MongoDB text search introduction"})
db.blogs.insert({_id:2,title:"MongoDB text index",content:"this is ae MongoDB text index  introduction"})
db.blogs.insert({_id:3,title:"MongoDB text operators",content:"this is ae MongoDB text query  introduction"})

2.创建Text Index。

只有拥有text index的collection才支持全文检索;
每个collection只能拥有一个text index;
Text index可以包含任何的string类型、string数组类型的字段;
Text index可以包含多个字段;
执行如下新建text index的语句
db.blogs.ensureIndex({title:"text",content:"text")

3.执行简单的全文检索

db.blogs.find({$text:{$search:"index"}})
 
4.查询包含index或者operators的记录

db.blogs.find({$text:{$search:"index operators"}})

5.查询包含mongodb但是不包含search的记录

db.blogs.find({$text:{$search:"mongodb -search"}})

6.查询包含text search词组的记录

db.blogs.find({$text:{$search:"\"text search\""}})

7.使用权重排序搜索结果

默认情况下全文检索返回的结果是无序的;
每次全文检索MongoDB会针对文档的匹配程度为每个document计算一个相对的分数;
MongoDB提供了$meta textScore来支持全文检索的分数
db.blogs.find( {$text:{$search:"mongodb index"}}, {score:{$meta:"textScore"}} ).sort({score:{$meta:"textScore"}}) 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值