For searching a specific word, use /searchWord/ with regex. Let us create a collection with documents −
> db.demo221.insertOne({"Details":{"StudentName":"Chris","StudentAge":21}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee15d03d395bdc213472b") } > db.demo221.insertOne({"Details":{"StudentName":"John","StudentAge":20}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee16503d395bdc213472c") } > db.demo221.insertOne({"Details":{"StudentName":"Bob","StudentAge":22}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee16b03d395bdc213472d") } > db.demo221.insertOne({"Details":{"StudentName":"john","StudentAge":24}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee17303d395bdc213472e") }
Display all documents from a collection with the help of find() method −
> db.demo221.find();
This will produce the following output −
{ "_id" : ObjectId("5e3ee15d03d395bdc213472b"), "Details" : { "StudentName" : "Chris", "StudentAge" : 21 } } { "_id" : ObjectId("5e3ee16503d395bdc213472c"), "Details" : { "StudentName" : "John", "StudentAge" : 20 } } { "_id" : ObjectId("5e3ee16b03d395bdc213472d"), "Details" : { "StudentName" : "Bob", "StudentAge" : 22 } } { "_id" : ObjectId("5e3ee17303d395bdc213472e"), "Details" : { "StudentName" : "john", "StudentAge" : 24 } }
Following is the query to fetch a specific record with name “John” −
> db.demo221.find({"Details.StudentName":/John/});
This will produce the following output −
{ "_id" : ObjectId("5e3ee16503d395bdc213472c"), "Details" : { "StudentName" : "John", "StudentAge" : 20 } }