1.作用与语法描述
复制代码
1
2
3{ <field>: { $regex: /pattern/, $options: '<options>' } } { <field>: { $regex: 'pattern', $options: '<options>' } } { <field>: { $regex: /pattern/<options> } }
复制代码
1{ <field>: /pattern/<options> }
2.案例演示
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47{ "_id" : ObjectId("5d305b1c4857fc49c0c14c81"), "order" : "QQ00001", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b3b4857fc49c0c14c82"), "order" : "QQ00001", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b404857fc49c0c14c83"), "order" : "QQ00002", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b454857fc49c0c14c84"), "order" : "QQ00002", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b4b4857fc49c0c14c85"), "order" : "QQ00003", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b4f4857fc49c0c14c86"), "order" : "QQ00003", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305bb74857fc49c0c14c87"), "order" : "QQ00003", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"shenzheng\" origincode=\"QQ0003\" /></Body></Response>"" }, { "_id" : ObjectId("5d305bd14857fc49c0c14c88"), "order" : "QQ00002", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"zhongshan\" origincode=\"QQ0002\" /></Body></Response>"" }, { "_id" : ObjectId("5d305be94857fc49c0c14c89"), "order" : "QQ00001", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"zhongshan\" origincode=\"QQ0001\" /></Body></Response>"" }
复制代码
1db.dbtestregex.find({data:{ $regex:/<Head>OK</Head>/}})
复制代码
1
2
3
4
5
6
7{ "_id" : ObjectId("5d305bb74857fc49c0c14c87"), "order" : "QQ00003", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"shenzheng\" origincode=\"QQ0003\" /></Body></Response>""}, { "_id" : ObjectId("5d305bd14857fc49c0c14c88"), "order" : "QQ00002", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"zhongshan\" origincode=\"QQ0002\" /></Body></Response>""}, { "_id" : ObjectId("5d305be94857fc49c0c14c89"), "order" : "QQ00001", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"zhongshan\" origincode=\"QQ0001\" /></Body></Response>""}
复制代码
1db.dbtestregex.find({data:/<Head>OK</Head>/})
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19{ "_id" : ObjectId("5d305bb74857fc49c0c14c87"), "order" : "QQ00003", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"shenzheng\" origincode=\"QQ0003\" /></Body></Response>"" }, { "_id" : ObjectId("5d305bd14857fc49c0c14c88"), "order" : "QQ00002", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"zhongshan\" origincode=\"QQ0002\" /></Body></Response>"" }, { "_id" : ObjectId("5d305be94857fc49c0c14c89"), "order" : "QQ00001", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>OK</Head><Body><OrderResponse filter_mmm=\"2\" destcity=\"zhongshan\" origincode=\"QQ0001\" /></Body></Response>"" }
复制代码
1db.dbtestregex.find({data:{ $regex:'<Head>OK</Head>'}})
3.类关系型数据库中的 not like 功能实现
复制代码
1
2db.dbtestregex.find({data:{ $not: /<Head>OK</Head>/ }})
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16{ "_id" : ObjectId("5d305b1c4857fc49c0c14c81"), "order" : "QQ00001", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b3b4857fc49c0c14c82"), "order" : "QQ00001", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b404857fc49c0c14c83"), "order" : "QQ00002", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b454857fc49c0c14c84"), "order" : "QQ00002", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b4b4857fc49c0c14c85"), "order" : "QQ00003", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}, { "_id" : ObjectId("5d305b4f4857fc49c0c14c86"), "order" : "QQ00003", "data" : ""<?xml version='1.0' encoding='UTF-8'?><Response service=\"OrderService\"><Head>ERR</Head><ERROR>重复下单</ERROR></Response>""}
最后
以上就是开朗过客最近收集整理的关于MongoDB的模糊查询操作(类关系型数据库的 like 和 not like)的全部内容,更多相关MongoDB的模糊查询操作(类关系型数据库的内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复