Skip to content

Comparison Operators

Estimated time to read: 1 minute

Overview

There are comparison operators for use within query operators to locate data within the database, these are:

  • $eq is EQual to
  • $ne is Not Equal to
  • $gt is Greater Than
  • $lt is Less Than
  • $gte is Greater Than or Equal to
  • $lte is Less Than or Equal to

The syntax for use of these operators is:

{ <field> : { <operator> : <valuer>} }

Comparison Operator Examples

The following code blocks show example usage of comparison operators within MongoDB:

Find all documents where the tripduration was less than or equal to 70 seconds and the usertype was not Subscriber: db.trips.find({ "tripduration": { "$lte" : 70 }, "usertype": { "$ne": "Subscriber" } }).pretty()

Find all documents where the tripduration was less than or equal to 70 seconds and the usertype was Customer using a redundant equality operator: db.trips.find({ "tripduration": { "$lte" : 70 }, "usertype": { "$eq": "Customer" }}).pretty()

Find all documents where the tripduration was less than or equal to 70 seconds and the usertype was Customer using the implicit equality operator:

db.trips.find({ "tripduration": { "$lte" : 70 }, "usertype": "Customer" }).pretty()

db.zips.find( {"pop" : {"$lte" : 1000} } )