Mongodb OR Operation - MongoDB Tutorial



Mongodb OR Operation

  • In the find() method , the mongodb - OR operation displays the data that has only one condition to be satisfied in order to display the certain data.
  • In mongoDB we need to perform the or operation means we need to use $or keyword.

Syntax :

db.mycol.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty() 

Sample Query :

> db.wikitechy4.find(
                   {
                      $or:
                         [
                           {"author":"jln"},
                           {"articlecount":"50"}
                         ]
                     }
             ).pretty()
{
        "_id" : ObjectId("5731e2eadaa4a0bc932a2ffb"),
        "websitename" : "www.wikitechy.com",
        "details" : "next row inserted",
        "articlecount" : "40",
        "author" : "jln"
}
{
        "_id" : ObjectId("5731f734daa4a0bc932a2ffd"),
        "websitename" : "www.wikitechy.com",
        "details" : "last row inserted",
        "articlecount" : "40",
        "author" : "jln"
}
{
        "_id" : ObjectId("5731f73fdaa4a0bc932a2ffe"),
        "websitename" : "www.wikitechy.com",
        "details" : "last row inserted",
        "articlecount" : "50",
        "author" : "jln"
}
>

MongoDb


Output

MongoDb
  1. Here in this output, we display the data from the collection “wikitechy4” where only the author name “jln” data’s are being displayed.
  2. Here in this output, we display the data from the Collection “wikitechy4” where only the author name “jln” data’s are being displayed.
  3. Here in this output, we display the data from the Collection “wikitechy4” where only the author name as “jln” & the article count as “50” constrain data’s will only be displayed using find() rich query.


Related Searches to Mongodb OR operation - MongoDB Tutorial