MongoDB $or OperatorMongoDB provides a variety of logical query operators. The $or operator is one of those operators. The $or operator performs logical "OR operations" on an array of one or more expressions. This operator is only used to retrieve the documents that match at least one of the given expressions in the array. The $OR operator is used to find multiple expressions in a single query that requires only one matching criterion in the document. Multiple keys and values can be used with the $or operator.
Syntax:Examples:In the following examples, we are working with: Example 1: MongoDB Logical $or Operator: In this example, we are only retrieving the data of students whose Course is "MCA" or batch_year is 2018. Output: >db.sutdent.find({$or: [{Course : "MCA"}, {batch_year : 2018}]}).pretty() { "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"), "name" : "Mick", "Coruse" : "btech", "batch_year" : 2018, "language" : ["c++", "java", "python"], "personal_details" : { "Father_name" : "Jonny", "phone_no" : 8895321456, "age" : 23, "gender" : "Male", "City" : "NewYork", } } { "_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"), "name" : "Jonny", "Coruse" : "MCA", "batch_year" : 2019, "language" : ["C#", "java", "PHP"], "personal_details" : { "Father_name" : "Thomas", "phone_no" : 7845123698, "age" : 24, "gender" : "Male", "City" : "London", } } Example 2: MongoDB Logical $or Operator (retrieving the data in the embedded documents): In this example, we are only retrieving those student's documents whose City is "London" or age is 20. Output: >db.student.find({$or: [{"personal.age": 20}, {"personal.City": "London"}]}).pretty() { "_id" : ObjectId("56254d4fdf2222265r4g12ds3d691"), "name" : "Zoya", "Course" : "BCA", "batch_year" : 2020, "language" : ["C#", "JavaScript"], "personal_details" : { "Father_name" : "Henry", "phone_no" : 9874563698, "age" : 20, "gender" : "Female", "City" : "London", } } { "_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"), "name" : "Jonny", "Course" : "MCA", "batch_year" : 2019, "language" : ["C#", "java", "PHP"], "personal_details" : { "Father_name" : "Thomas", "phone_no" : 7845123698, "age" : 24, "gender" : "Male", "City" : "London", } } Example 3: Matching values in an array using $or operator: In this example, we are only retrieving those students' documents that match at least one value in the given array. Output: >db.student.find({$or: [{language: {$in: ["c++", "java", "HTML"]}}]}).pretty() { "_id" : ObjectId("56254d4fdf2222265r4g12ds3d65f"), "name" : "Mick", "Course" : "btech", "batch_year" : 2018, "language" : ["c++", "java", "python"], "personal_details" : { "Father_name" : "Jonny", "phone_no" : 8895321456, "age" : 23, "gender" : "Male", "City" : "NewYork", } } { "_id" : ObjectId("56254d4fdf2222265r4g12ds3d655"), "name" : "Jonny", "Course" : "MCA", "batch_year" : 2019, "language" : ["C#", "java", "PHP"], "personal_details" : { "Father_name" : "Thomas", "phone_no" : 7845123698, "age" : 24, "gender" : "Male", "City" : "London", } } { "_id" : ObjectId("56254d4fdf2222265r4g12ds3d665"), "name" : "Mia", "Course" : "btech", "batch_year" : 2020, "language" : ["HTML", "CSS", "PHP"], "personal_details" : { "Father_name" : "Leo", "phone_no" : 6312547896, "age" : 22, "gender" : "Female", "City" : "Manchester", } } Next TopicMongoDB $in Operator |