是的,为此,请使用$indexOfCP运算符以及聚合框架。首先让我们创建一个包含文档的集合-
> db.patterDemo.insertOne( { "ClientName": "John", "ClientWebsiteName":"webbuziness.com/John/business" } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea40acef71edecf6a1f68d") } > db.patterDemo.insertOne( { "ClientName": "Carol", "ClientWebsiteName":"solvecoding.com/business" } ); { "acknowledged" : true, "insertedId" : ObjectId("5cea40acef71edecf6a1f68e") }
以下是在find()方法的帮助下显示集合中所有文档的查询-
> db.patterDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5cea40acef71edecf6a1f68d"), "ClientName" : "John", "ClientWebsiteName" : "abcd.com" } { "_id" : ObjectId("5cea40acef71edecf6a1f68e"), "ClientName" : "Carol", "ClientWebsiteName" : "pqrs.com" }
以下是将字段值用作$regex中的模式的查询-
> db.patterDemo.aggregate([ { "$match": { "$expr": { "$ne": [ { "$indexOfCP": ["$ClientWebsiteName", "$ClientName"] }, -1 ] } }} ]);
这将产生以下输出-
{ "_id" : ObjectId("5cea40acef71edecf6a1f68d"), "ClientName" : "John", "ClientWebsiteName" : "abcd.com" }