在MongoDB中构建(转义)正则表达式?
为此,请find()
与//i一起使用。让我们创建一个包含文档的集合-
> db.demo696.insertOne({Message:"/Good/"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d664551299a9f98c9391") } > db.demo696.insertOne({Message:"(good)"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d67a551299a9f98c9392") } > db.demo696.insertOne({Message:"/Bye/"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d68b551299a9f98c9393") } > db.demo696.insertOne({Message:"(GOOD)"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d693551299a9f98c9394") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo696.find();
这将产生以下输出-
{ "_id" : ObjectId("5ea6d664551299a9f98c9391"), "Message" : "/Good/" } { "_id" : ObjectId("5ea6d67a551299a9f98c9392"), "Message" : "(good)" } { "_id" : ObjectId("5ea6d68b551299a9f98c9393"), "Message" : "/Bye/" } { "_id" : ObjectId("5ea6d693551299a9f98c9394"), "Message" : "(GOOD)" }
以下是转义regexp的查询-
> db.demo696.find({Message:/good/i});
这将产生以下输出-
{ "_id" : ObjectId("5ea6d664551299a9f98c9391"), "Message" : "/Good/" } { "_id" : ObjectId("5ea6d67a551299a9f98c9392"), "Message" : "(good)" } { "_id" : ObjectId("5ea6d693551299a9f98c9394"), "Message" : "(GOOD)" }