Skip to content

Instantly share code, notes, and snippets.

@kowsheek
Created May 24, 2018 15:06
Show Gist options
  • Save kowsheek/ffd78672ddca3b3eaf06586218098173 to your computer and use it in GitHub Desktop.
Save kowsheek/ffd78672ddca3b3eaf06586218098173 to your computer and use it in GitHub Desktop.
W3D4
const chalk = require('chalk');
const tasks = [
{ title: 'Hustle', status: 'complete' },
{ title: 'Grind', status: 'pending' },
{ title: 'Repeat', status: 'pending' },
];
const statusChalker = {
pending: chalk.bold.red,
complete: chalk.bold.green
}
function showTasks() {
console.log(chalk.bold.underline( tasks.length + ' tasks'));
tasks.forEach(function (task, index) {
console.log(statusChalker[task.status](task.title));
});
}
function addTask(title) {
return tasks.push({ title: title, status: 'pending' });
}
function removeTask(index) {
return tasks.splice(index, 1);
}
showTasks();
addTask('Do more');
showTasks();
addTask('Move fast');
addTask('Break things');
removeTask(2);
showTasks();
const chalk = require('chalk');
const Mongo = require('mongodb');
const MongoClient = Mongo.MongoClient;
const MONGODB_URI = "mongodb://127.0.0.1:27017/lhl-tasks";
const statusChalker = {
pending: chalk.bold.red,
complete: chalk.bold.green
}
function showTasks(mongoInstance) {
return new Promise(function (resolve, reject) {
mongoInstance.collection("tasks").find({}).toArray(function (err, tasks) {
if (err) {
console.log(chalt.red(err));
return reject(err);
}
console.log(tasks.length + ' tasks');
tasks.forEach(function (task) {
console.log(task._id + ': ' + statusChalker[task.status](task.title));
});
return resolve();
});
});
}
function addTask(mongoInstance, title) {
return new Promise(function (resolve, reject) {
mongoInstance.collection("tasks").insertOne({
title: title,
status: 'pending'
}, function (err, result) {
if (err) {
console.log(chalt.red(err));
return reject();
}
return resolve();
});
});
}
function removeTask(mongoInstance, _id) {
return new Promise(function (resolve, reject) {
mongoInstance.collection("tasks").deleteOne({
_id: Mongo.ObjectID(_id)
}, function (err, result) {
if (err) {
console.log(chalt.red(err));
return reject();
}
return resolve();
});
});
}
MongoClient.connect(MONGODB_URI, (err, mongoInstance) => {
if (err) {
throw err;
}
console.log(`Connected to MongoDb: ${MONGODB_URI}`);
showTasks(mongoInstance)
.then(function () {
return addTask(mongoInstance, 'Item at ' + new Date().toTimeString())
})
.then(function () {
return showTasks(mongoInstance);
})
.then(function () {
return removeTask(mongoInstance, '5b05c566c71b2a20785bf5ec');
}).then(function () {
mongoInstance.close();
process.exit();
return
});
});

Background

  1. Need for persistence
  2. Persistence in the real world
  3. Persistence in computers
    • In memory
    • Text/JSON/binary files
    • Databases
  4. Types of databases

MongoDB

  1. NoSql database
    • Server > Databases > Collections > Documents
    • Collections
      • Misconceptions
  2. Interfacing with the database
    • Command line/shell
    • API/driver
  3. Commands
show dbs
use <db>

# read
db.<collection>.find({"email": "jack@belly.com" })
db.<collection>.findOne({"profile.name": "Jack Belly" })

# write 
db.createCollection('collectionName')
db.<collection>.insertOne({...})
db.<collection>.updateOne({...})
db.<collection>.deleteOne({...})

  1. _id field is automatically created and indexed
  2. Using MongoDb in NodeJS
    • mongodb package
    • Creating a connection
    • Closing a connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment