MongoDB 查询搜索字段值中类似 “@email” 的字符串
使用 MongoDB find() 搜索电子邮件字符串。我们创建一个带有文档的集合 -
> db.demo727.insertOne({UserId:"[email protected]"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab375f43417811278f5898") } > db.demo727.insertOne({UserId:"[email protected]"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab376043417811278f5899") } > db.demo727.insertOne({UserId:"[email protected]"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab376143417811278f589a") }
使用 find() 方法显示集合中的所有文档 -
> db.demo727.find();
这将生成以下输出 -
{ "_id" : ObjectId("5eab375f43417811278f5898"), "UserId" : "[email protected]" } { "_id" : ObjectId("5eab376043417811278f5899"), "UserId" : "[email protected]" } { "_id" : ObjectId("5eab376143417811278f589a"), "UserId" : "[email protected]" }
以下是查询 @email 类字符串 -
> db.demo727.find({"UserId":/@email/i});
这将生成以下输出 -
{ "_id" : ObjectId("5eab375f43417811278f5898"), "UserId" : "[email protected]" } { "_id" : ObjectId("5eab376143417811278f589a"), "UserId" : "[email protected]" }
广告