Let us first create a collection with documents −
> 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")
}Display all documents from a collection with the help of find() method −
> db.demo186.find();
This will produce the following output −
{ "_id" : ObjectId("5e399d769e4f06af55199808"), "UserEmailId" : "[email protected]", "UserName" : "John" }
{ "_id" : ObjectId("5e399d879e4f06af55199809"), "UserEmailId" : "[email protected]", "UserName" : "chris" }
{ "_id" : ObjectId("5e399d979e4f06af5519980a"), "UserEmailId" : "[email protected]", "UserName" : "David" }Following is the query for case insensitive search −
> var userMailId = [ /[email protected]/i, /[email protected]/i ]
> db.demo186.find({
... '$or': [
... { 'UserEmailId': { '$in': userMailId} },
... { 'UserName': 'John' }
... ]
...})This will produce the following output −
{ "_id" : ObjectId("5e399d769e4f06af55199808"), "UserEmailId" : "[email protected]", "UserName" : "John" }
{ "_id" : ObjectId("5e399d979e4f06af5519980a"), "UserEmailId" : "[email protected]", "UserName" : "David" }