Write an equality in MongoDB without using $eq operator


Let us first create a collection with documents -

> db.operatorDemo.insertOne({"StudentSubject":["MongoDB","MySQL","Java"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cef94eaef71edecf6a1f6a2")
}
> db.operatorDemo.insertOne({"StudentSubject":["Java","C","C++"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cef94faef71edecf6a1f6a3")
}

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

> db.operatorDemo.find().pretty();

Output

{
   "_id" : ObjectId("5cef94eaef71edecf6a1f6a2"),
   "StudentSubject" : [
      "MongoDB",
      "MySQL",
      "Java"
   ]
}
{
   "_id" : ObjectId("5cef94faef71edecf6a1f6a3"),
   "StudentSubject" : [
      "Java",
      "C",
      "C++"
   ]
}

Following is the query for an equality without $eq operator:

> db.operatorDemo.find({StudentSubject:"MongoDB"});

Output

{ "_id" : ObjectId("5cef94eaef71edecf6a1f6a2"), "StudentSubject" : [ "MongoDB", "MySQL", "Java" ] }
raja
Published on 14-Jun-2019 14:09:55
Advertisements