logo_learn_stats

MongoDB: How one can Significance the OR ($or) Operator in Queries

Posted on
banner 336x280

You’ll be able to virtue the $or operator in MongoDB to question for paperwork that meet one in all a couple of standards.

This operator makes use of please see ordinary syntax:

banner 468x60
db.myCollection.to find({
  "$or": [
    {"field1": "hello"},
    {"field2": {$gte : 10}}
  ]
})

This actual instance reveals all paperwork within the assortment titled myCollection the place field1 is the same as “hello” or field2 has a price more than or equivalent to ten.

Please see examples display how one can virtue this syntax in apply with a set groups with please see paperwork:

db.groups.insertOne({staff: "Mavs", issues: 30, rebounds: 8})
db.groups.insertOne({staff: "Mavs", issues: 35, rebounds: 12})
db.groups.insertOne({staff: "Spurs", issues: 20, rebounds: 7})
db.groups.insertOne({staff: "Spurs", issues: 25, rebounds: 5})
db.groups.insertOne({staff: "Spurs", issues: 23, rebounds: 9})

Instance 1: Significance OR Operator with Two Grounds

Please see code presentations how one can to find all paperwork within the groups assortment the place the “team” garden is the same as “Spurs” or the price within the “points” garden is bigger than or equivalent to 31:

db.groups.to find({
  "$or": [
    {"team": "Spurs"},
    {"points": {$gte: 31}}
  ]
})

This question returns please see paperwork:

{ _id: ObjectId("62018750fd435937399d6b6f"),
  staff: 'Mavs',
  issues: 35,
  rebounds: 12 }
{ _id: ObjectId("62018750fd435937399d6b70"),
  staff: 'Spurs',
  issues: 20,
  rebounds: 7 }
{ _id: ObjectId("62018750fd435937399d6b71"),
  staff: 'Spurs',
  issues: 25,
  rebounds: 5 }
{ _id: ObjectId("62018750fd435937399d6b72"),
  staff: 'Spurs',
  issues: 23,
  rebounds: 9 } 

Realize that every file within the output accommodates “Spurs” within the staff garden or a price more than or equivalent to 31 within the issues garden. 

Instance 2: Significance OR Operator with Extra Than Two Grounds

Please see code presentations how one can to find all paperwork within the groups assortment the place the “team” garden is the same as “Mavs” or the price within the “points” garden is bigger than or equivalent to twenty-five or the price within the “rebounds” garden is lower than 8:

db.groups.to find({
  "$or": [
    {"team": "Mavs"},
    {"points": {$gte: 25}},
    {"rebounds": {$lt: 8}}
  ]
})

This question returns please see file:

{ _id: ObjectId("62018750fd435937399d6b6e"),
  staff: 'Mavs',
  issues: 30,
  rebounds: 8 }
{ _id: ObjectId("62018750fd435937399d6b6f"),
  staff: 'Mavs',
  issues: 35,
  rebounds: 12 }
{ _id: ObjectId("62018750fd435937399d6b70"),
  staff: 'Spurs',
  issues: 20,
  rebounds: 7 }
{ _id: ObjectId("62018750fd435937399d6b71"),
  staff: 'Spurs',
  issues: 25,
  rebounds: 5 }

Realize that every of those paperwork satisfies a number of of the 3 standards:

  • The “team” garden is the same as “Mavs
  • The “points” garden has a price more than or equivalent to 25
  • The “rebounds” garden has a price lower than 8

Observe: You’ll be able to to find your complete documentation for the $or serve as right here.

Alternative Assets

Please see tutorials provide an explanation for how one can carry out alternative ordinary operations in MongoDB:

MongoDB: How one can Significance the AND Operator in Queries
MongoDB: How one can Test if Garden Comprises a Fable
MongoDB: How one can Significance a “NOT IN” Question
MongoDB: How one can Question for “not null” in Particular Garden

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *