Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / routes.js
Created May 27, 2014 04:53
config/routes.js
module.exports.routes = {
// By default, your root route (aka home page) points to a view
// located at `views/home/index.ejs`
//
// (This would also work if you had a file at: `/views/home.ejs`)
'/': {
controller: 'HomeController',
action: 'hello_world'
}
@saintc0d3r
saintc0d3r / HomeController.js
Last active August 29, 2015 14:01
HomeController.js
/**
* HomeController
*
* @module :: Controller
* @description :: A set of functions called `actions`.
*
* Actions contain code telling Sails how to respond to a certain type of request.
* (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL)
*
* You can configure the blueprint URLs which trigger these actions (`config/controllers.js`)
@saintc0d3r
saintc0d3r / GuestBookImageRepository.cs
Created November 17, 2013 05:11
[C#][AWS][S3][MsTest] A sample of AWS S3's File Repository.
using System;
using System.IO;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
namespace GuestBook.Repository.Aws.S3
{
class GuestBookImageRepository : S3RepositoryBase, IGuestBookImageRepository
{