是否可以在$ regex中使用MongoDB字段值作为模式?
是的,您可以使用$indexOfCP来将字段值用作模式。让我们创建一个包含文档的集合-
> db.demo88.insertOne(
... {
... "Name": "Chris",
... "PassoutYear": "2020",
... "websiteName": "chris.shop.com/Carol-2020-"
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2c14c471bf0181ecc422b1")
}
> db.demo88.insertOne(
... {
... "Name": "David",
... "PassoutYear": "2010",
... "websiteName": "david.bigshop.com/David-2010-"
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2c14c571bf0181ecc422b2")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo88.find();
这将产生以下输出-
{ "_id" : ObjectId("5e2c14c471bf0181ecc422b1"), "Name" : "Chris", "PassoutYear" : "2020", "websiteName" : "chris.shop.com/Carol-2020-" }
{ "_id" : ObjectId("5e2c14c571bf0181ecc422b2"), "Name" : "David", "PassoutYear" : "2010", "websiteName" : "david.bigshop.com/David-2010-" }以下是将字段值用作$regex中的模式的查询-
> db.demo88.aggregate([ { "$match": { "$expr": { "$ne": [ { "$indexOfCP": ["$websiteName", "$Name"] }, -1 ] } }} ])这将产生以下输出-
{ "_id" : ObjectId("5e2c14c571bf0181ecc422b2"), "Name" : "David", "PassoutYear" : "2010", "websiteName" : "david.bigshop.com/David-2010-" }热门推荐
1 如何训练狗狗握手
10 新员工寄语祝福语简短
11 新婚敬酒祝福语大全简短
12 怎么训练狗狗
13 训练狗狗上厕所
14 春蚕老师祝福语简短励志
15 高考好运如愿祝福语简短
16 战哥过年祝福语简短
17 中考感谢朋友祝福语简短
18 长沙旅游祝福语大全简短