MongoDB 正则表达式
示例
MongoDB 使用 $regex 操作符来设置匹配字符串的正则表达式。
> db.col.find()
{ "_id" : ObjectId("56c6bbef64799370c0ef358a"), "x" : "hello world", "tags" : [ "a", "b" ] }
{ "_id" : ObjectId("56c6bbfe64799370c0ef358b"), "x" : "hi world", "tags" : [ "b" ] }
{ "_id" : ObjectId("56c6bc9e64799370c0ef358c"), "x" : "nothing" }
> db.col.find({x:/world/})
{ "_id" : ObjectId("56c6bbfe64799370c0ef358b"), "x" : "hi world", "tags" : [ "b" ] }
{ "_id" : ObjectId("56c6bbef64799370c0ef358a"), "x" : "hello world", "tags" : [ "a", "b" ] }
> db.col.find({x:{$regex:"world"}})
{ "_id" : ObjectId("56c6bbfe64799370c0ef358b"), "x" : "hi world", "tags" : [ "b" ] }
{ "_id" : ObjectId("56c6bbef64799370c0ef358a"), "x" : "hello world", "tags" : [ "a", "b" ] }
上面后两种操作是等价的。
不区分大小写
> db.col.find({x:{$regex:"world", $options:"$i"}})
{ "_id" : ObjectId("56c6bd9964799370c0ef358d"), "x" : "hihi WORLD" }
{ "_id" : ObjectId("56c6bbfe64799370c0ef358b"), "x" : "hi world", "tags" : [ "b" ] }
{ "_id" : ObjectId("56c6bbef64799370c0ef358a"), "x" : "hello world", "tags" : [ "a", "b" ] }
or
> db.col.find({x:/world/i})
{ "_id" : ObjectId("56c6bd9964799370c0ef358d"), "x" : "hihi WORLD" }
{ "_id" : ObjectId("56c6bbfe64799370c0ef358b"), "x" : "hi world", "tags" : [ "b" ] }
{ "_id" : ObjectId("56c6bbef64799370c0ef358a"), "x" : "hello world", "tags" : [ "a", "b" ] }
数组使用正则表达式
> db.col.find({tags:/b/})
{ "_id" : ObjectId("56c6bbef64799370c0ef358a"), "x" : "hello world", "tags" : [ "a", "b" ] }
{ "_id" : ObjectId("56c6bbfe64799370c0ef358b"), "x" : "hi world", "tags" : [ "b" ] }
{ "_id" : ObjectId("56c6bedf64799370c0ef358e"), "tags" : [ "abc", "m" ] }
可见数组中包含字符‘b’的都找出来了(包括“abc”)。
正则中包含变量
需要用eval将组合的字符串进行转换
> v="world"
world > db.col.find({x:eval("/" + v + "/i")})
{ "_id" : ObjectId("56c6bd9964799370c0ef358d"), "x" : "hihi WORLD" }
{ "_id" : ObjectId("56c6bbfe64799370c0ef358b"), "x" : "hi world", "tags" : [ "b" ] }
{ "_id" : ObjectId("56c6bbef64799370c0ef358a"), "x" : "hello world", "tags" : [ "a", "b" ] }
MongoDB 正则表达式的更多相关文章
- MongoDB正则表达式
MongoDB 使用 $regex 操作符来设置匹配字符串的正则表达式. 1. 搜索包含某关键字的内容: db.posts.find({post_text:{$regex:"w3cschoo ...
- MongoDB 正则表达式查询
正则表达式查询 $regex 注:^ 取反的意思 用特殊的转义字符需要在前面加一个斜杠 通过 ^取反 ,再通过$not取反,就可获得只包含一种类型的数据 \\d 数字 \\s 空格 \\ ...
- mongodb 初学 目录
mongodb 初学 索引 啦啦啦 MongoDB 教程 NoSQL 简介 MongoDB 简介 Windows 平台安装 MongoDB Linux平台安装MongoDB mongodb 在 Ubu ...
- MongoDB 高级教程
MongoDB 关系 MongoDB 的关系表示多个文档之间在逻辑上的相互联系. 文档间可以通过嵌入和引用来建立联系. MongoDB 中的关系可以是: 1:1 (1对1) 1: N (1对多) N: ...
- 使用Requests+正则表达式爬取猫眼TOP100电影并保存到文件或MongoDB,并下载图片
需要着重学习的地方:(1)爬取分页数据时,url链接的构建(2)保存json格式数据到文件,中文显示问题(3)线程池的使用(4)正则表达式的写法(5)根据图片url链接下载图片并保存(6)MongoD ...
- MongoDB用PCRE正则表达式
介绍 下面说明 PCRE 所支持的正则表达式的语法和语义.Perl 文档和很多其它书中也解说了正则表达式,有的书中有很多例子.Jeffrey Friedl 写的“Mastering Regular E ...
- 【翻译】MongoDB指南/CRUD操作(四)
[原文地址]https://docs.mongodb.com/manual/ CRUD操作(四) 1 查询方案(Query Plans) MongoDB 查询优化程序处理查询并且针对给定可利用的索引选 ...
- MongoDB学习笔记六—查询下
查询内嵌文档 数据准备 > db.blog.find().pretty() { "_id" : ObjectId("585694e4c5b0525a48a441b5 ...
- MongoDB学习笔记五—查询上
数据准备 { , "goods_name" : "KD876", "createTime" : ISODate("2016-12- ...
随机推荐
- HttpRequestDeviceUtils
import javax.servlet.http.HttpServletRequest;public class HttpRequestDeviceUtils { /**Wap网关Via头信息中特有 ...
- Python常用函数笔记
1.lambda lambda其实就是一条语句,lambda(x):body.x是lambda函数的参数,参数可以有任意多个(包括可选参数);body是函数体,只能是一个表达式,并且直接返回该表达式的 ...
- thinkphp开发技巧经验分享
thinkphp开发技巧经验分享 www.111cn.net 编辑:flyfox 来源:转载 这里我给大家总结一个朋友学习thinkphp时的一些笔记了,从变量到内置模板引擎及系统变量等等的笔记了,同 ...
- 详解在bash脚本中如何获取自身路径
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 这是stac ...
- Class 实现IDisposing方法
public class MyResourceHog : IDisposable { // 已经被处理过的标记 private bool _alreadyDisposed = false; ...
- truncate
css .ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } php $truncatedTitl ...
- 验证码 mewebstudio/captcha
composer require mews/captcha https://github.com/mewebstudio/captcha
- P1371 NOI元丹
luogu月赛的题 本来想爆搜,但是经过ly大佬的点拨,明白这是一个dp. 我们定义dp[n]为从n开始的可行串的数目,具体如下:如果n为'I',则是从n开始有多少个I,如果n为'O',既是从n开始有 ...
- Java Tool
PS参数详解 http://blog.csdn.net/hanner_cheung/article/details/6081440 JVM 参数 JVM调优总结 -Xms -Xmx -Xmn –Xss ...
- Blender to XPS(blender 2.7x Internal materials)
Things we are gonna need are Blender 2.7x www.blender.org/ XPS tools addon for Blender A model made ...