Lec 8
Lec 8
Output:
1 Laptop 25000
2 TV 40000
3 Router 2000
4 Scanner 5000
5 Printer 9000
Output:
[{"_id":"67474acc43db2b3fe1a0b771","ProductID":2,"Name":"TV","price":40000]
3
}
// search by key value
var keyValue = { Name: "TV" };
await listbykeyvalue(client, "mydatabase", "products", keyValue);
MongoDB Operators
The operators are used in find() method to apply filter
Sr.No MongoDB Operator Description
1 $eq equal to (==)
2 $gt greater than (>)
3 $gte greater than or equal to (>=)
4 $in if equal to any value in array
5 $lt less than (<)
6 $lte less than or equal to (<=)
7 $ne not equal to (!=)
8 $nin if not equal to any value in array
4
Example
const {MongoClient} = require('mongodb');
async function main() {
const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
await client.connect();
// Make the appropriate DB calls
// Create a single new listing
await fetchdocs(client, "mydatabase", "products");
} finally {
// Close the connection to the MongoDB cluster
await client.close();
}
}
main().catch(console.error);
async function fetchdocs(client, dbname, colname) {
const result = await
client.db(dbname).collection(colname).find({ "price": { $gt: 10000 } }).toArray();
console.log(JSON.stringify(result));
}
Output:
[{"_id":"67474acc43db2b3fe1a0b770","ProductI
D":1,"Name":"Laptop","price":25000},{"_id":"67
474acc43db2b3fe1a0b771","ProductID":2,"Name ":"TV","price":40000}]
5
The $and as well as $or operators are available for compound logical
expressions
db.collection.find($and:[{"key1":"value1"}, {"key2":"value2"}])
Example
async function fetchdocs(client, dbname, colname) {
const result = await client.db(dbname).collection(colname).find({
$and: [{ "price": { $gt: 1000 } }, { "price": { $lt: 10000 } }]
}).toArray();
console.log(JSON.stringify(result));
}
Output
[{"_id":"67474acc43db2b3fe1a0b772","Product
ID":3,"Name":"Router","price":2000},{"_id":
"67474acc43db2b3fe1a0b773","ProductID":4,"N
ame":"Scanner","price":5000},{"_id":"67474a
cc43db2b3fe1a0b774","ProductID":5,"Name":"P rinter","price":9000}]
Output
{
_id: new ObjectId('67474acc43db2b3fe1a0b772'),
ProductID: 3,
Name: 'Router',
price: 2000
}{
_id: new ObjectId('67474acc43db2b3fe1a0b773'),
ProductID: 4,
Name: 'Scanner',
price: 5000
}{
_id: new ObjectId('67474acc43db2b3fe1a0b774'),
ProductID: 5,
Name: 'Printer',
price: 9000
}{
_id: new ObjectId('67474acc43db2b3fe1a0b770'),
ProductID: 1,
Name: 'Laptop',
price: 25000
}{
_id: new ObjectId('67474acc43db2b3fe1a0b771'),
ProductID: 2,
Name: 'TV',
price: 40000
}
8
deleteOne()
async function deldocs(client, dbname, colname) {
var myqry = { Name: "TV" };
const result = await client.db(dbname).collection(colname).deleteOne(myqry);
console.log("Document Deleted");
}
deleteMany()
async function deldocs(client, dbname, colname) {
var myqry = {"price": { $gt: 10000 } };
const result = await client.db(dbname).collection(colname).deleteMany(myqry);
console.log("Documents Deleted");
}
Drop Collection
drop()
async function dropcol(client, dbname, colname) {
const result = await client.db(dbname).collection(colname).drop();
console.log("Collection dropped ");
}
dropCollection()
async function dropcol(client, dbname, colname) {
const result = await client.db(dbname).dropCollection(colname);
console.log("Collection dropped");
}
9
updateOne()
async function updatedocs(client, dbname, colname) {
var qry = { ProductID: 3 };
var vals = { $set: { Name: "Router", price: 2750 } };
const result = await client.db(dbname).collection(colname).updateOne(qry,
vals);
console.log("Document updated");
}
updateMany()
async function updatemanydocs(client, dbname, colname) {
var qry = { Name: /er$/ };
var vals = { $inc: { price: 125 } };
const result = await client.db(dbname).collection(colname).updateMany(qry,
vals);
console.log("Documents updated");
}