MongoDB 查询针对特定不区分大小写搜索
我们首先使用文档创建一个集合 -
> db.demo186.insertOne({"UserEmailId":"[email protected]","UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d769e4f06af55199808") } > db.demo186.insertOne({"UserEmailId":"[email protected]","UserName":"chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d879e4f06af55199809") } > db.demo186.insertOne({"UserEmailId":"[email protected]","UserName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d979e4f06af5519980a") }
使用 find() 方法从集合显示所有文档 -
> db.demo186.find();
这将产生以下输出 -
{ "_id" : ObjectId("5e399d769e4f06af55199808"), "UserEmailId" : "[email protected]", "UserName" : "John" } { "_id" : ObjectId("5e399d879e4f06af55199809"), "UserEmailId" : "[email protected]", "UserName" : "chris" } { "_id" : ObjectId("5e399d979e4f06af5519980a"), "UserEmailId" : "[email protected]", "UserName" : "David" }
以下是对不区分大小写搜索的查询 -
> var userMailId = [ /[email protected]/i, /[email protected]/i ] > db.demo186.find({ ... '$or': [ ... { 'UserEmailId': { '$in': userMailId} }, ... { 'UserName': 'John' } ... ] ...})
这将产生以下输出 -
{ "_id" : ObjectId("5e399d769e4f06af55199808"), "UserEmailId" : "[email protected]", "UserName" : "John" } { "_id" : ObjectId("5e399d979e4f06af5519980a"), "UserEmailId" : "[email protected]", "UserName" : "David" }
广告