npm install mongodb
const { MongoClient } = require('mongodb') const client = new MongoClient("mongodb://0.0.0.0:27017") const database = client.db("demo") console.log("Connected to Demo database!") client.close()
Function insertOne() takes a JavaScript object and inserts a new Document into books collection.
const { MongoClient } = require('mongodb') const client = new MongoClient("mongodb://0.0.0.0:27017") async function addBook(title, author, price) { try { const database = client.db("demo") // connect to Database const booksCollection = database.collection('books') // get collection const book = { title: `${title}`, author: `${author}`, price: price } const result = await booksCollection.insertOne(book) // insert document console.log(result) // show result } catch (ex) { console.log("Error -->" + ex) } finally { await client.close() } } // Call function with details of a book addBook('No rules rules', 'Reed Hastings', 800)
{ acknowledged: true, insertedId: new ObjectId('65c06f3ab700b8592d49cbcb') }
const { MongoClient } = require('mongodb') const client = new MongoClient("mongodb://0.0.0.0:27017") async function updateBook(title, price) { try { const database = client.db("demo") const booksCollection = database.collection('books') const query = { title : `${title}`} const newValues = { $set: { price: `${price}` } } const result = await booksCollection.updateOne(query, newValues) console.log(result) } catch (ex) { console.log("Error -->" + ex) } finally { await client.close() } } updateBook('No rules rules', 700)
{ acknowledged: true, modifiedCount: 1, upsertedId: null, upsertedCount: 0, matchedCount: 1 }
const { MongoClient } = require('mongodb') const client = new MongoClient("mongodb://0.0.0.0:27017") async function deleteBook(title) { try { const database = client.db("demo") const booksCollection = database.collection('books') const query = { title : `${title}`} const result = await booksCollection.deleteOne(query) if (result.deletedCount === 1) // Deleted successfully console.log(`Deleted ${title}`) else console.log(`Title - ${title} - not found!`) } catch (ex) { console.log("Error -->" + ex) } finally { await client.close() } } deleteBook('No rules rules')
const { MongoClient } = require('mongodb') const client = new MongoClient("mongodb://0.0.0.0:27017") async function listCostlyBooks(price) { try { const database = client.db("demo") const booksCollection = database.collection('books') // value 1 selects the attribute and 0 omits it const options = { projection: { _id: 0, title: 1, author : 1, price: 1} } const query = {price: { $gt: price }} const books = booksCollection.find(query, options) // returns FindCursor // Access one document (book) at a time from FindCursor for await (const book of books) { console.log(book) } } catch (ex) { console.log("Error -->" + ex) } finally { await client.close() } } listCostlyBooks(600)
{ title: 'No Filter: The Inside Story of Instagram', author: 'Sarah Frier', price: 700 } { title: 'No rules rules', author: 'Reed Hastings', price: 800 }