This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { "_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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "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": [], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const data = require('./data.json'); | |
| console.log(data.games); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| try { | |
| await db.collection('games').createIndex({ | |
| name: 1, | |
| platform: 1 | |
| }, { | |
| unique: true | |
| }); | |
| } catch (e) { | |
| console.log('Defined Index already exists, skipping ....'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const readAllDocs = async(db) => { | |
| try { | |
| const docs = await db.collection('games').find({}); | |
| return docs.toArray(); | |
| } catch (e) { | |
| console.log(e.toString()); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const searchInDb = async (db, searchQuery) => { | |
| try { | |
| const docs = await db.collection('games').find(searchQuery); | |
| return docs.toArray(); | |
| } catch (e) { | |
| console.log(e.toString()); | |
| } | |
| }; |