MongoDB find by multiple array items?

MongoDBDatabaseBig Data Analytics

You can use $all operator to find by multiple array items. To understand the concept, let us create a collection with the document.

The query to create a collection with a document is as follows −

> db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith",
   "StudentCoreSubject":["Compiler","Operating System","Computer Networks"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7ef07b559dd2396bcfbfc4")
}
> db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"Carol","StudentLastName":"Taylor",
   "StudentCoreSubject":["MongoDB","MySQL","SQL Server"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7ef09d559dd2396bcfbfc5")
}
> db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"Bob","StudentLastName":"Taylor",
   "StudentCoreSubject":["MongoDB","MySQL","SQL Server"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7ef0c7559dd2396bcfbfc6")
}
> db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"David","StudentLastName":"Johnson",
   "StudentCoreSubject":["Compiler","Operating System","Computer Networks"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c7ef0f2559dd2396bcfbfc7")
}

Display all documents from a collection with the help of find() method. The query is as follows −

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

The following is the output −

{
   "_id" : ObjectId("5c7ef07b559dd2396bcfbfc4"),
   "StudentFirstName" : "John",
   "StudentLastName" : "Smith",
   "StudentCoreSubject" : [
      "Compiler",
      "Operating System",
      "Computer Networks"
   ]
}
{
   "_id" : ObjectId("5c7ef09d559dd2396bcfbfc5"),
   "StudentFirstName" : "Carol",
   "StudentLastName" : "Taylor",
   "StudentCoreSubject" : [
      "MongoDB",
      "MySQL",
      "SQL Server"
   ]
}
{
   "_id" : ObjectId("5c7ef0c7559dd2396bcfbfc6"),
   "StudentFirstName" : "Bob",
   "StudentLastName" : "Taylor",
   "StudentCoreSubject" : [
      "MongoDB",
      "MySQL",
      "SQL Server"
   ]
}
{
   "_id" : ObjectId("5c7ef0f2559dd2396bcfbfc7"),
   "StudentFirstName" : "David",
   "StudentLastName" : "Johnson",
   "StudentCoreSubject" : [
      "Compiler",
      "Operating System",
      "Computer Networks"
   ]
}

Here is the query to find by multiple array items −

> db.findByMultipleArrayDemo.find({ StudentCoreSubject: { $all: ["Compiler", "Computer Networks"] }}).pretty();

The following is the output displaying record with array items "Compiler" and "Computer Networks −

{
   "_id" : ObjectId("5c7ef07b559dd2396bcfbfc4"),
   "StudentFirstName" : "John",
   "StudentLastName" : "Smith",
   "StudentCoreSubject" : [
      "Compiler",
      "Operating System",
      "Computer Networks"
   ]
}
{
   "_id" : ObjectId("5c7ef0f2559dd2396bcfbfc7"),
   "StudentFirstName" : "David",
   "StudentLastName" : "Johnson",
   "StudentCoreSubject" : [
      "Compiler",
      "Operating System",
      "Computer Networks"
   ]
}
raja
Published on 28-Mar-2019 12:37:44
Advertisements