Skip to content

Instantly share code, notes, and snippets.

@saintc0d3r
saintc0d3r / HomeController.coffee
Created May 27, 2014 05:06
HomeController.coffee
module.exports = {
##
# Action blueprints:
# `/home/index`
# `/home`
#
hello_world = req, res -> res.json({hello: 'world'})
}
@saintc0d3r
saintc0d3r / error_stacktrace_1
Created May 27, 2014 05:23
error_stacktrace_1
##
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at /home/wendysa/Documents/Excercises/demo_sails/node_modules/sails/node_modules/include-all/index.js:96:53
@saintc0d3r
saintc0d3r / error_stacktrace_2
Created May 27, 2014 05:29
error_stacktrace_2
hello_world: (req, res) -> res.json({hello: 'world'})
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at /home/wendysa/Documents/Excercises/demo_sails/node_modules/sails/node_modules/include-all/index.js:96:53
@saintc0d3r
saintc0d3r / hello_express_swig_mongodb.js
Last active August 29, 2015 14:02
Hello World in Express +Swig + MongoDb
// Initialise Express
var express = require('express'),
app = express();
// Initialise Consolidate
var consolidate = require('consolidate');
// setup swig as the view html engine
app.engine("html", consolidate.swig);
app.set("view engine", "html");
@saintc0d3r
saintc0d3r / multikeys_index_demo.js
Last active August 29, 2015 14:02
Multi keys index in MongoDB
// Prepare sample data on the Teachers collection
db.teachers.insert({'_id': 0, 'name': 'Maria'})
db.teachers.insert({'_id': 1, 'name': 'Jill'})
db.teachers.insert({'_id': 2, 'name': 'Anna'})
db.teachers.insert({'_id': 3, 'name': 'Lara'})
// Prepare sample data on the Students collection
db.students.insert({'_id':0, 'name':'Lisa', 'teachers': [0,1]})
db.students.insert({'_id':1, 'name':'Paulene', 'teachers': [0,1,3]})
db.students.insert({'_id':2, 'name':'Joey', 'teachers': [1,2,3]})
@saintc0d3r
saintc0d3r / tree_structure_mongodb.js
Created June 22, 2014 10:16
A sample of Tree Documents structure in Mongodb
// Creating root category
db.categories.insert({'_id': 0, 'name': 'Computer Hardware' })
// Creating 1st level category
db.categories.insert({'_id': 1, 'name': 'Computer Components', 'parent_id': 0, 'ancestors': [0]})
// Creating 2nd level categories
db.categories.insert({'_id': 2, 'name': 'CPUs', 'parent_id': 1, 'ancestors': [1, 0]})
db.categories.insert({'_id': 3, 'name': 'Motherboards', 'parent_id': 1, 'ancestors': [1, 0]})
db.categories.insert({'_id': 4, 'name': 'Memory', 'parent_id': 1, 'ancestors': [1, 0]})
@saintc0d3r
saintc0d3r / add_index.js
Last active August 29, 2015 14:03
Add indexes on a collection's columns in mongodb
// Let the collection's name is 'students' & we want to speed up the query's performance on the 'students' collection when we find documents in the collection by 'student_id'.
// In order to achieve this, we'll add an index for the 'student_id' column where we want to index it by ascending order.
db.students.ensureIndex({'student_id': 1})
// Another case is when we want to speed up query which takes both 'student_id' & 'class' columns where the class would be descending order.
db.students.ensureIndex({'student_id':1, 'class':-1})
@saintc0d3r
saintc0d3r / view_drop_indexes.js
Created June 26, 2014 13:41
View & Dropping indexes in mongodb
// Let's say, there is a database in the mongodb server named as 'school'. Let's use this database.
use school
// Let's view a list of available indexes on the 'school' database.
db.system.indexes.find()
// Let the 'school' db has a collection named as 'students'. Then, we want to list indexes on a particular collection ('students')
db.students.getIndexes()
// Supposed, there is an index for one of 'students' columns, let's say '_id'. Then, we want to delete this index
@saintc0d3r
saintc0d3r / multi_key_index.js
Last active August 29, 2015 14:03
Muilti key Indexes on mongodb
// Let's say we have a database named as 'foo'
use foo
// On the foo database, there is a collection named as 'bar' & we'll add a record on 'bar' collection
db.bar.insert({'a':1, 'b':5})
// Now, we are going to add indexes for both a & b
db.bar.ensureIndex({'a':1, 'b': -1})
// Let's see which index the bar uses when querying it a or/and b. Notice what is the values of "cursor" & "isMultiKey" in the returned result
@saintc0d3r
saintc0d3r / unique_index.js
Created June 27, 2014 14:33
Unique Index on a mongodb's collection
// Let's say there is a fruits collection where it contains a pear document
db.fruits.insert({'name':'pear'})
// Then, we want to prevent another document with name = pear can be inserted into fruits collection
// In the other words, we want to make the name property become unique
db.fruits.ensureIndex({'name':1}, {'unique':true})
// Let's push a new document that has different name than to the existing one
db.fruits.insert({'name':'apel'})