Created
July 4, 2014 12:37
-
-
Save jeffdonthemic/3fc55e245fb61f44563e to your computer and use it in GitHub Desktop.
Mongo scripts used for Chowfinder app
This file contains 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
if(process.env.VCAP_SERVICES){ | |
var env = JSON.parse(process.env.VCAP_SERVICES); | |
var mongo = env['mongodb-1.8'][0]['credentials']; | |
} | |
else{ | |
var mongo = { | |
"hostname":"localhost", | |
"port":27017, | |
"username":"", | |
"password":"", | |
"name":"", | |
"db":"db" | |
} | |
} | |
var generate_mongo_url = function(obj){ | |
obj.hostname = (obj.hostname || 'localhost'); | |
obj.port = (obj.port || 27017); | |
obj.db = (obj.db || 'test'); | |
if(obj.username && obj.password){ | |
return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db; | |
} | |
else{ | |
return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db; | |
} | |
} | |
var mongourl = generate_mongo_url(mongo); |
This file contains 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
// creates a new facility for a location | |
app.post('/v.1/locations/:location_id/facilities', function(req, res){ | |
// add the location id to the json | |
var facility = req.body; | |
facility['location'] = req.params.location_id; | |
require('mongodb').connect(mongourl, function(err, conn){ | |
conn.collection('facilities', function(err, coll){ | |
coll.insert( facility, {safe:true}, function(err){ | |
res.writeHead(200, { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*" | |
}); | |
res.end(JSON.stringify(facility)); | |
}); | |
}); | |
}); | |
}); |
This file contains 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
// creates a location in the 'locations' collection | |
app.post('/v.1/locations', function(req, res){ | |
require('mongodb').connect(mongourl, function(err, conn){ | |
conn.collection('locations', function(err, coll){ | |
coll.insert( req.body, {safe:true}, function(err){ | |
res.writeHead(200, { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*" | |
}); | |
res.end(JSON.stringify(req.body)); | |
}); | |
}); | |
}); | |
}); |
This file contains 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
// returns a specific location by id | |
app.get('/v.1/locations/:location_id', function(req, res){ | |
var ObjectID = require('mongodb').ObjectID; | |
require('mongodb').connect(mongourl, function(err, conn){ | |
conn.collection('locations', function(err, coll){ | |
coll.findOne({'_id':new ObjectID(req.params.location_id)}, function(err, document) { | |
res.writeHead(200, { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*" | |
}); | |
res.end(JSON.stringify(document)); | |
}); | |
}); | |
}); | |
}); |
This file contains 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
// returns a specific location by id | |
app.get('/v.1/locations/:location_id', function(req, res){ | |
var ObjectID = require('mongodb').ObjectID; | |
require('mongodb').connect(mongourl, function(err, conn){ | |
conn.collection('locations', function(err, coll){ | |
coll.findOne({'_id':new ObjectID(req.params.location_id)}, function(err, document) { | |
res.writeHead(200, { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*" | |
}); | |
res.end(JSON.stringify(document)); | |
}); | |
}); | |
}); | |
}); |
This file contains 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
// creates a location in the 'locations' collection | |
app.post('/v.1/locations', function(req, res){ | |
require('mongodb').connect(mongourl, function(err, conn){ | |
conn.collection('locations', function(err, coll){ | |
coll.insert( req.body, {safe:true}, function(err){ | |
res.writeHead(200, { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*" | |
}); | |
res.end(JSON.stringify(req.body)); | |
}); | |
}); | |
}); | |
}); |
This file contains 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
// returns list of locations | |
app.get('/v.1/locations', function(req, res){ | |
require('mongodb').connect(mongourl, function(err, conn){ | |
conn.collection('locations', function(err, coll){ | |
coll.find(function(err, cursor) { | |
cursor.toArray(function(err, items) { | |
res.writeHead(200, { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*" | |
}); | |
res.end(JSON.stringify(items)); | |
}); | |
}); | |
}); | |
}); | |
}); |
This file contains 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
// updates a facility | |
app.put('/v.1/locations/:location_id/facilities/:facility_id', function(req, res){ | |
var ObjectID = require('mongodb').ObjectID; | |
require('mongodb').connect(mongourl, function(err, conn){ | |
conn.collection('facilities', function(err, coll){ | |
coll.findAndModify({'_id':new ObjectID(req.params.facility_id)}, [['name','asc']], { $set: req.body }, {}, function(err, document) { | |
res.writeHead(200, { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*" | |
}); | |
res.end(JSON.stringify(document)); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment