MongoDB中regex用法介绍(2)

使用s选项来执行查询,则会让逗号. 匹配所有字符,包括换行符,下文查询了description列中m开头,且后面包含line字符串的结果:
db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before    line" }
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }

如果不包含s,则会返回:

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

Part8:x的使用

以下示例使用x选项忽略空格和注释,用#表示注释,并以匹配模式中的\ n结尾:
var pattern = "abc #category code\n123 #item number"
db.products.find( { sku: { $regex: pattern, $options: "x" } } )

查询的结果是:
{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }

可以看出,其忽略了abc与#category的空格以及#category与code的空格,实际执行的查询是sku是abc123的结果。

——总结——

通过这几个案例,我们能够了解到MongoDB中的regex用法,以及其可选参数$option每个选项的含义和用法。

更多MongoDB相关教程见以下内容

MongoDB文档、集合、数据库简介 

MongoDB 3分片部署及故障模拟验证 

Linux CentOS 6.5 yum安装MongoDB 

CentOS 7 yum方式快速安装MongoDB 

MongoDB的查询操作 

在 Azure 虚拟机上快速搭建 MongoDB 集群 

MongoDB复制集原理 

MongoDB 3.4 远程连接认证失败 

Ubuntu 16.04中安装MongoDB3.4数据库系统 

MongoDB权威指南第2版PDF完整带书签目录 下载见

MongoDB 的详细介绍请点这里
MongoDB 的下载地址请点这里

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/32ed158cd5d9ea048ad59cc1ebd16022.html