Alternative of MongoDB operator $eq to get similar result


For writing an equality, you can simply use find() along with match value. Let us create a collection with documents −

> db.demo145.insertOne({"ListOfNames":["Chris","David","Mike"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e32f37bfdf09dd6d08539bb")
}
> db.demo145.insertOne({"ListOfNames":["Bob","John"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e32f384fdf09dd6d08539bc")
}

Display all documents from a collection with the help of find() method −

> db.demo145.find();

This will produce the following output −

{ "_id" : ObjectId("5e32f37bfdf09dd6d08539bb"), "ListOfNames" : [ "Chris", "David", "Mike" ] }
{ "_id" : ObjectId("5e32f384fdf09dd6d08539bc"), "ListOfNames" : [ "Bob", "John" ] }

Following is the query to implement MongoDB operator $eq −

> db.demo145.find({"ListOfNames":"John"});

This will produce the following output −

{ "_id" : ObjectId("5e32f384fdf09dd6d08539bc"), "ListOfNames" : [ "Bob", "John" ] }

Updated on: 31-Mar-2020

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements