Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
MongoDB query to implement OR operator in find()
Let us create a collection with documents −
> db.demo78.insertOne({"Name1":"Chris","Name2":"Mike"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2bd99c71bf0181ecc4228f")
}
> db.demo78.insertOne({"Name1":"Bob","Name2":"Carol"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2bd9ac71bf0181ecc42290")
}
> db.demo78.insertOne({"Name1":"David","Name2":"Sam"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2bd9b671bf0181ecc42291")
}
> db.demo78.insertOne({"Name1":"Jace","Name2":"John"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e2bd9bf71bf0181ecc42292")
}
Display all documents from a collection with the help of find() method −
> db.demo78.find();
This will produce the following output −
{ "_id" : ObjectId("5e2bd99c71bf0181ecc4228f"), "Name1" : "Chris", "Name2" : "Mike" }
{ "_id" : ObjectId("5e2bd9ac71bf0181ecc42290"), "Name1" : "Bob", "Name2" : "Carol" }
{ "_id" : ObjectId("5e2bd9b671bf0181ecc42291"), "Name1" : "David", "Name2" : "Sam" }
{ "_id" : ObjectId("5e2bd9bf71bf0181ecc42292"), "Name1" : "Jace", "Name2" : "John" }
Following is the query to implement OR operator in find() −
> db.demo78.find({$or: [{"Name1": "Bob"}, {Name2: "John"}] });
This will produce the following output −
{ "_id" : ObjectId("5e2bd9ac71bf0181ecc42290"), "Name1" : "Bob", "Name2" : "Carol" }
{ "_id" : ObjectId("5e2bd9bf71bf0181ecc42292"), "Name1" : "Jace", "Name2" : "John" } Advertisements
