Skip to content

Instantly share code, notes, and snippets.

View ShawonAshraf's full-sized avatar
🐈
I'm simply not there.

Shawon Ashraf ShawonAshraf

🐈
I'm simply not there.
View GitHub Profile
@ShawonAshraf
ShawonAshraf / data.json
Created August 3, 2019 20:38
Code for mongodb-nodejs-medium article
{ "_id" : { "$oid" : "5bfd70ebdeae4f03e8177d92" }, "name" : "Red Dead Redemption 2", "publisher" : "Rockstar", "platform" : "PS4" }
{ "_id" : { "$oid" : "5bfd70ebdeae4f03e8177d93" }, "name" : "Red Dead Redemption 2", "publisher" : "Rockstar", "platform" : "XBOXONE" }
{ "_id" : { "$oid" : "5bfd70ebdeae4f03e8177d94" }, "name" : "Forza Horizon 2", "publisher" : "Microsoft Studios", "platform" : "XBOXONE" }
{ "_id" : { "$oid" : "5bfd70ebdeae4f03e8177d95" }, "name" : "Forza Horizon 3", "publisher" : "Microsoft Studios", "platform" : "XBOXONE" }
{ "_id" : { "$oid" : "5bfd70ebdeae4f03e8177d96" }, "name" : "Forza Horizon 4", "publisher" : "Microsoft Studios", "platform" : "XBOXONE" }
{ "_id" : { "$oid" : "5bfd70ebdeae4f03e8177d97" }, "name" : "Halo Wars 2", "publisher" : "Microsoft Studios", "platform" : "XBOXONE" }
{ "_id" : { "$oid" : "5bfd70ebdeae4f03e8177d98" }, "name" : "Rise of The Tomb Raider", "publisher" : "Square Enix", "platform" : "XBOXONE" }
{ "_id" : { "$oid" : "5bfd70ebdeae4f03e8177d99" }, "name" : "Ha
@ShawonAshraf
ShawonAshraf / package.json
Created August 3, 2019 20:33
Code for mongodb-nodejs-medium article
{
"name": "mongo-start-nodejs-medium",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
@ShawonAshraf
ShawonAshraf / app.js
Created August 3, 2019 20:31
Code for mongodb-nodejs-medium article
const mongodb = require('mongodb');
const data = require('./data.json');
const games = data.games; // that's what we need actually
const url = 'mongodb://localhost:27017/learnMongo';
const dbName = 'learnMongo'; // let's say that's our database name
// not needed however
// use the new url parser to avoid warning messages
@ShawonAshraf
ShawonAshraf / read-data.js
Created August 3, 2019 20:29
Code for mongodb-nodejs-medium article
const data = require('./data.json');
console.log(data.games);
@ShawonAshraf
ShawonAshraf / add-multiple.js
Created August 3, 2019 20:26
Code for mongodb-nodejs-medium article
const data = require('./data.json');
const games = data.games; // that's what we need actually
const addMultipleDocs = async(docs, db) => {
try {
// const db = client.db();
await db.collection('games').insertMany(docs);
console.log('Multiple Docs Added!');
@ShawonAshraf
ShawonAshraf / add-single.js
Created August 3, 2019 20:24
Code for mongodb-nodejs-medium article
// add a single element
const singleDoc = {
name: 'Red Dead Redemption 2',
platform: 'PS4',
publisher: 'Rockstar'
};
const addOneDocument = async(doc, db) => {
try {
// const db = client.db();
@ShawonAshraf
ShawonAshraf / create-index.js
Created August 3, 2019 20:21
Code for mongodb-nodejs-medium article
try {
await db.collection('games').createIndex({
name: 1,
platform: 1
}, {
unique: true
});
} catch (e) {
console.log('Defined Index already exists, skipping ....');
}
@ShawonAshraf
ShawonAshraf / add-doc.js
Created August 3, 2019 20:19
Code for mongodb-nodejs-medium article
const addOneDocument = async(doc, db) => {
try {
// const db = client.db();
await db.collection('games').insertOne(doc);
console.log('Document has been updated! ...');
} catch (e) {
console.log(e.toString());
}
};
@ShawonAshraf
ShawonAshraf / read-all-docs.js
Created August 3, 2019 20:18
Code for mongodb-nodejs-medium article
const readAllDocs = async(db) => {
try {
const docs = await db.collection('games').find({});
return docs.toArray();
} catch (e) {
console.log(e.toString());
}
};
@ShawonAshraf
ShawonAshraf / search.js
Created August 3, 2019 20:17
Code for mongodb-nodejs-medium article
const searchInDb = async (db, searchQuery) => {
try {
const docs = await db.collection('games').find(searchQuery);
return docs.toArray();
} catch (e) {
console.log(e.toString());
}
};