How to Query MongoDB Documents in JS

This lesson makes the following assumptions:

1. You have properly connected to your database, and declared the following:

const { MongoClient } = require("mongodb");
const uri = "mongodb://localhost:27017"
const client = new MongoClient(uri, { useUnifiedTopology: true })

async function runQuery() {
    await client.connect()
    const database = client.db('test')
    const collection = database.collection('inventory')

     // insert query here
    const results =  


    // close connection
    await client.close()

}
runQuery()

2. You have added the following test-data:

await collection.insertMany([
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);

Finding ALL documents in a Collection

const results = await collection.find({})
await results.forEach(doc => console.log(doc))

Finding a Single Match

const results = await collection.findOne({item:"planner"})
console.log(results)

Doing so will return the document, NOT a a cursor so there is no need to iterate over it to display results

Finding SOME documents in a Collection

const results = await collection.find( { status: "D" } )
await results.forEach(doc => console.log(doc))
const results = collection.find( { status: "D", item: "planner" } )
await results.forEach(doc => console.log(doc))

Finding SOME documents using OR

const results = await collection.find(
  { $or: [
          { item: "Planner" },
          { item: "Notebook" }
         ]
  }
)
await results.forEach(doc => console.log(doc))
const results = await collection.find(
  { $or: [
          { status: "A" },
          { item: "Notebook" }
         ]
  }
)
await results.forEach(doc => console.log(doc))

Finding SOME documents using RANGES

const results = await collection.find( { <field>: { $gt: <value1>, $lt: <value2> } } );
const results = await collection.find( { status: "A", qty: { $lt: 30 } } )
await results.forEach(doc => console.log(doc))

Querying for properties IN a set

const results = await collection.find( { status: { $in: ["A", "B", "C"] } } )
const results = await collection.find( { item: { $in: ["Journal", "Notebook", "Paper"] } } )
await results.forEach(doc => console.log(doc))

Querying Nested Documents

const results = await collection.find({ status: "A", size: { h: 14, w: 21, uom: "cm" } })
await results.forEach(doc => console.log(doc))
const results = await collection.find({ "size.h": 14 });
await results.forEach(doc => console.log(doc))
const results = await collection.find({ status: "A", "size.h": 14 })
await results.forEach(doc => console.log(doc))

Range Queries in Nested Documents

{
    "_id" : ObjectId("5b631aff2f6ff13721a2e38b"),
    "item" : "journal",
    "status" : "A",
    "size" : {
        "h" : 14,
        "w" : 21,
        "uom" : "cm"
    }
}
const results = await collection.find({ "size.h":  { $gt: 10 } })
await results.forEach(doc => console.log(doc))
const results = await collection.find({ "size.h":  { $gt: 10, $lt: 100 } })
await results.forEach(doc => console.log(doc))

Range Queries in Nested Documents using OR

const results = await collection.find( {
     status: "A",
     $or: [
            { qty: { $lt: 30 } },
            { "size.h": { $gt: 10 } }
          ]
    }
)
await results.forEach(doc => console.log(doc))

List of Comparisons for Ranges

Name Description
$eq Matches values that are equal to a specified value.
$gt Matches values that are greater than a specified value.
$gte Matches values that are greater than or equal to a specified value
$in Matches any of the values specified in an array
$lt Matches values that are less than a specified value.
$lte Matches values that are less than or equal to a specified value.
$ne Matches all values that are not equal to a specified value.
$nin Matches none of the values specified in an array.

 Previous Next 

Outline

[menu]

/