To find a MongoDB document through a word, use find() and set the word like −
word/i
Let us create a collection with documents −
> db.demo212.insertOne({"details":[{"Name":"John Doe"}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3e2c7603d395bdc21346ff")
}
> db.demo212.insertOne({"details":[{"Name":"Chris Brown"}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3e2c8003d395bdc2134700")
}
> db.demo212.insertOne({"details":[{"Name":"Robert doe"}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3e2c8a03d395bdc2134701")
}Display all documents from a collection with the help of find() method −
> db.demo212.find();
This will produce the following output −
{ "_id" : ObjectId("5e3e2c7603d395bdc21346ff"), "details" : [ { "Name" : "John Doe" } ] }
{ "_id" : ObjectId("5e3e2c8003d395bdc2134700"), "details" : [ { "Name" : "Chris Brown" } ] }
{ "_id" : ObjectId("5e3e2c8a03d395bdc2134701"), "details" : [ { "Name" : "Robert doe" } ] }Following is the query to find a MongoDB document through a word −
> db.demo212.find({"details.Name":/doe/i});This will produce the following output −
{ "_id" : ObjectId("5e3e2c7603d395bdc21346ff"), "details" : [ { "Name" : "John Doe" } ] }
{ "_id" : ObjectId("5e3e2c8a03d395bdc2134701"), "details" : [ { "Name" : "Robert doe" } ] }