Skip to content

Instantly share code, notes, and snippets.

@luca4startup
Last active December 21, 2015 20:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luca4startup/6364728 to your computer and use it in GitHub Desktop.
Save luca4startup/6364728 to your computer and use it in GitHub Desktop.
mongodb
// install mongoDB on the standard port: www.mongodb.org/downloads , extract
// hw1-1: include PATH, restore a database dump (/hw1/dump/...bson and other files...; dump directory just beneath current dir) with
~/../ tar xvf hw1.tar
~/../ echo $PATH; setenv ???
~/../hw1/ mongorestore dump
~/../hw1/ mongo
> help; show dbs; show collections; show users; use m101; db.hw1_1.findOne()
// hw1-2: install the NODE-MONGODB-NATIVE driver (This should create a "node_modules" directory with dependencies) anr run ../hw2/app.js
~/../hw1-2/ npm install mongodb ...............(-g does it globally)
~/../hw1-2/ node app.js .......................(example of connect db, findOne(), deciphering a message with a256)
// hw1-3: install dependencies in ../hw1-3/package.json (-> consolidate, express, crypto, mongodb, swig), run ../hw1-3/app.js and browser to localhost:8080
~/../hw1-3/ npm install ; node app.js .......................(example of connect db, findOne(), deciphering, basic swig view + routes)
'Express server started on port 8080'
// mongodb hw1-2&3 - DECIPHER algorithm
var express = require('express'), app = express(), cons = require('consolidate'), crypto = require('crypto'),
MongoClient = require('mongodb').MongoClient;
app.engine('html', cons.swig); app.set('view engine', 'html'); app.set('views', __dirname + '/views'); // ....................SWIG/VIEWS
// name is passed after deciphering: in views/hello.html contains only : <h1>Hello, {{name}}.</h1>
MongoClient.connect('mongodb://localhost:27017/m101', function(err, db) { if(err) throw err;
app.get('/', function(req, res){ // ................... ROUTE FOR "/"
var algorithm = 'aes256';
var encrypted_message = 'f36731a12e6130f0ed0bccbfd9bd6ebd';
db.collection('hw1_3').findOne(function(err, doc) {
if(err) throw err; if (!doc) { console.dir("No document found"); return db.close(); } // ....................err/!doc typical
var decipher = crypto.createDecipher(algorithm, doc['_id']);
var decrypted = decipher.update(encrypted_message, 'hex', 'utf8') + decipher.final('utf8');
return res.render('hello', { "name" : decrypted }); console.log("Answer: " + decrypted); // .... "hello" is the .html view
});
});
app.get('*', function(req, res){ return res.send('Page Not Found', 404); }); // ....................only lets "/" route above
app.listen(8080); console.log('Express server started on port 8080'); // ................. starts server on 8080
});
/* 2-1: "weather_data.csv" import into MongoDB as follows:
> mongoimport --type csv --headerline weather_data.csv -d weather -c data # -d databasename -c collectionname , see --headerline
> use weather ; db.data.findOne() # use it and see the structure o one element
"State" that recorded the lowest "Temperature" when the wind was coming from the west ("Wind Direction" between 180 and 360)
> db.data.find({"Wind Direction": {$lt:360, $gt: 180}}).sort({Temperature: 1}).limit(1).pretty()
2-2: Write a program that finds the document with the highest recorded temperature for each state, and adds a "month_high" field for that document, setting its value to true. Hint: If you select all the weather documents, you can sort first by state, then by temperature. Then you can iterate through the documents and know that whenever the state changes you have reached the highest temperature for that state. Use the validation script.
Reimport the data and try again (in case)
> mongo weather --eval "printjson(db.dropDatabase())" # in case REIMPORTING the db is needed
*/
// pendiente de copiar solución video y comparar con la mía
// Start the blog in hw2-3: npm install; node app.js . Express server listening on port 3000; http://localhost:3000/signup /login /logout /welcome ...
// database is "blog", collections are "users" "sessions" "posts" each with a DAO DataAccessObject in users.js sessions.js posts.js
// MODIFY users.js TODO HW 2.3 ---
var bcrypt = require('bcrypt');
function UsersDAO(db) { /* The UsersDAO must be constructed with a connected database object */
"use strict"; // ??????????????????????????? FIND IT ???????????????????????
/* If this constructor is called without the "new" operator, "this" points
* to the global object. Log a warning and call it correctly. */
if (false === (this instanceof UsersDAO)) { console.log('Warning: UsersDAO constructor called without "new" operator'); return new UsersDAO(db);}
var users = db.collection("users");
this.addUser = function(username, password, email, callback) {
// CALLED FROM SIGNUPFORM->sessions.js/HandleSignUp: given username/pwd/email returns USER DOCUMENT IF OK
"use strict";
var salt = bcrypt.genSaltSync(); var password_hash = bcrypt.hashSync(password, salt); // Generate password hash
var user = {'_id': username, 'password': password_hash}; // Create user document -> to be returned if ok
if (email != "") { user['email'] = email; } // Add email if set
// TODO: hw2.3 .... substituted: callback(Error("addUser Not Yet Implemented!"), null);
users.insert(user, function (err, result) { // RESULT IS A JAVASCRIPT OBJECT that has to be stringified to be printed (see slides)
"use strict";
if (!err) {console.log("Inserted new user"); console.dir(JSON.stringify(result)); return (callback(null, result[0])); } // RESULT[0] = user ???
return callback(err, null); // if error inserting
});
}
this.validateLogin = function(username, password, callback) {
"use strict";
function validateUserDoc(err, user) { // Callback to pass to MongoDB that validates a user document
"use strict";
if (err) return callback(err, null);
if (user) { if (bcrypt.compareSync(password, user.password)) { callback(null, user); }
else {var invalid_password_error = new Error("Invalid password");
invalid_password_error.invalid_password = true; callback(invalid_password_error, null); } // Set an extra field so we can distinguish this from a db error
}
else { var no_such_user_error = new Error("User: " + user + " does not exist");
no_such_user_error.no_such_user = true; callback(no_such_user_error, null); // Set an extra field so we can distinguish this from a db error
}
}
// TODO: hw2.3 ...substituted: callback(Error("validateLogin Not Yet Implemented!"), null);
users.findOne({ '_id' : username }, validateUserDoc); // just this line: call validate after looking for this username
}
}
module.exports.UsersDAO = UsersDAO;
MONGODB 2.2+ (a.b.c : b must be even for stable version) start server on por 27017 with mongod
MONGO SHELL
> help; show dbs; show collections; show users;
> use xxxdatabasexxx
Configure MongoDB:
/etc/mongodb.conf file in conjunction with the control script (/etc/init.d/mongodb or /etc/rc.d/mongodb, used by the system’s initialization process to start, restart or stop a daemon process).
DATA files in /var/lib/mongodb ; LOG files in /var/log/mongodb # ¿¿¿¿¿ /data/db/ ?????
Modify the access control rights to the /var/lib/mongodb and /var/log/mongodb directories if needed for other users.
Controlling MongoDB
Starting MongoDB: sudo service mongodb start #verify the log file at /var/log/mongodb/mongodb.log
Stopping MongoDB: sudo service mongodb stop
Restarting MongoDB: sudo service mongodb restart
# HISTORY OF INSTALLATION PROBLEMS
3 - tried downloading and intalled on AWS "per se": seemed like it was installed previously (¿?)
# http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
1 - tried on my computer 32bit ... doesn´t work #(see xxx thread !!)
download, tar xvf, mkdir ./data/db && chmod 777 ./data/db/ #for development#
export PATH=/users/lcarrion/mongodb-2.4.5/bin/:$PATH #change PATH with EXPORT, or modify /bash_profile
2 - AWS has an EBS option (and an "add-on" not free?) # instructions in http://www.murvinlai.com/nodejs--mongodb-on-aws.html
$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list
$ sudo apt-get update ; sudo apt-get install mongodb-10gen ; sudo service mongodb stop ; sudo /usr/bin/mongod -- dbpath /home/ubuntu/db/data
# (right AWS security group that opens up port 80 and 22)
# NODE
$ sudo apt-get update #https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
$ sudo apt-get install python-software-properties python g++ make ; sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update ; sudo apt-get install nodejs
# If you have previous version of node, then it is quite troublesome, you need to uninstall the previous node first:
$ sudo rm -r bin/node bin/node-waf include/node lib/node lib/pkgconfig/nodejs.pc share/man/man1/node.1
SHELL
&& executes the right-hand command of && only if the previous one succeeded.
|| executes the right-hand command of || only it the previous one failed.
; executes the right-hand command of ; always regardless whether the previous command succeeded or failed
# A word or line beginning with # causes that word and all remaining characters on that line to be ignored.
also:
<<COMMENT1
xxxx
COMMENT1
export PATH=/users/lcarrion/mongodb-2.4.5/bin/:$PATH #change PATH with EXPORT, or modify /bash_profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment