概述
1.作用与语法描述
{ <field>: { $regex: /pattern/, $options: '<options>' } } { <field>: { $regex: 'pattern', $options: '<options>' } } { <field>: { $regex: /pattern/<options> } }
{ <field>: /pattern/<options> }
2.案例演示
{ "_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>"" }
db.dbtestregex.find({data:{ $regex:/<Head>OK</Head>/}})
{ "_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>""}
db.dbtestregex.find({data:/<Head>OK</Head>/})
{ "_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>"" }
db.dbtestregex.find({data:{ $regex:'<Head>OK</Head>'}})
3.类关系型数据库中的 not like 功能实现
db.dbtestregex.find({data:{ $not: /<Head>OK</Head>/ }})
{ "_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的模糊查询操作(类关系型数据库的 like 和 not like)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复