0% found this document useful (0 votes)
2 views

Database Engineering Lab 8

This document outlines a lab exercise for Database Engineering focusing on querying data in MongoDB using the mongosh console. It includes steps for setting up a database and collection, inserting sample data, and performing queries using element operators such as $exists and $type. The document is part of a course for a B.Tech CSE student at UPES, Dehradun.

Uploaded by

abhinavgiri60
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Database Engineering Lab 8

This document outlines a lab exercise for Database Engineering focusing on querying data in MongoDB using the mongosh console. It includes steps for setting up a database and collection, inserting sample data, and performing queries using element operators such as $exists and $type. The document is part of a course for a B.Tech CSE student at UPES, Dehradun.

Uploaded by

abhinavgiri60
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DATABASE ENIGINEERING

LAB-8

Name- Abhinav Giri


Roll No.- R2142221345
SAP ID- 500110031
Course- B.Tech CSE(FSAI)
Semester- 6
Batch- 3

SCHOOL OF COMPUTER SCIENCE


UPES, DEHRADUN
Lab Exercise 5 - Using Element Operators to
Query Data in MongoDB (mongosh Console)

1. Launch the mongosh Console

2. Set Up a Database and Collection

Switch to a database:

Insert sample data into a collection:


db.inventory.insertMany([ { item: "laptop", qty: 10, price: 800, inStock: true }, { item: "mouse",
qty: 50, price: 20, color: "black" }, { item: "keyboard", qty: 25, price: 45, inStock: false },
{ item: "monitor", qty: 15, price: 150 }, { item: "speaker", qty: 30, dimensions: { length: 10,
width: 5, height: 7 } } ])
1. Query Using $exists
Find documents with the inStock field:

db.inventory.find({ inStock: { $exists: true } })

Find documents without the inStock field:


db.inventory.find({ inStock: { $exists: false } })
Query Using $type
Find documents where the price field is a number:

db.inventory.find({ price: { $type: "number" } }


Find documents where the dimensions field is an object:
db.inventory.find({ dimensions: { $type: "object" } })

Find documents where the color field is a string:


db.inventory.find({ color: { $type: "string" } })

You might also like