more mongo work

v1.18.x
Baris Usakli 11 years ago
parent e862a1c4cc
commit 10474f8e2a

@ -32,6 +32,7 @@
db.createCollection('sets', function(err, collection) { db.createCollection('sets', function(err, collection) {
}); });
callback(err); callback(err);
}); });
// look up how its done in mongo // look up how its done in mongo
@ -74,20 +75,27 @@
//hashes //hashes
module.setObject = function(key, data, callback) { module.setObject = function(key, data, callback) {
console.log('SET OBJECT CALLED', key, data);
data['_key'] = key; data['_key'] = key;
db.collection('objects').insert(data, {w:1}, function(err, result) { db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err, result) {
console.log('SET OBJECT COMPLETE', err, result);
callback(err, result); callback(err, result);
}); });
} }
module.setObjectField = function(key, field, value, callback) { module.setObjectField = function(key, field, value, callback) {
db.collection('objects').update(); var data = {};
data[field] = value;
db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err, result) {
console.log('SET OBJECT COMPLETE', err, result);
callback(err, result);
});
} }
module.getObject = function(key, callback) { module.getObject = function(key, callback) {
console.log('calling findOne'); console.log('GET OBJECT', key);
db.collection('objects').findOne({_key:key}, function(err, item) { db.collection('objects').findOne({_key:key}, function(err, item) {
console.log(item); console.log('RETURNING OBJECT', item);
callback(err, item); callback(err, item);
}); });
} }
@ -106,7 +114,7 @@
var _fields = {}; var _fields = {};
for(var i=0; i<fields.length; ++i) { for(var i=0; i<fields.length; ++i) {
_fields[fields[i]] = 1; _fields[fields[i]] = 'baris';
} }
db.collection('objects').findOne({_key:key}, {fields:_fields}, function(err, item) { db.collection('objects').findOne({_key:key}, {fields:_fields}, function(err, item) {
@ -172,10 +180,19 @@
} }
module.getSetMembers = function(key, callback) { module.getSetMembers = function(key, callback) {
console.log('getting set members'); console.log('GETTING SET MEMBERS', key);
db.collection('sets').findOne({_key:key}, function(err, data) { db.collection('sets').findOne({_key:key}, function(err, data) {
console.log('derp', err, data); if(err) {
callback(err, data); return callback(err);
}
if(!data) {
console.log('GOT SET MEMBERS', []);
callback(null, []);
} else {
console.log('GOT SET MEMBERS', data);
callback(null, data);
}
}); });
} }

@ -78,6 +78,66 @@ var DebugRoute = function(app) {
}); });
}); });
}); });
app.get('/mongo', function(req, res) {
var db = require('./../database');
var objectKey = 'someotherObj';
function setObject(callback) {
db.setObject(objectKey, {name:'baris', 'lastname':'usakli', age:3}, function(err, result) {
console.log('setObject return ', result);
callback(err, {'setObject':result});
});
}
function getObject(callback) {
db.getObject(objectKey, function(err, data) {
console.log('getObject return ', data);
callback(err, {'getObject':data});
});
}
function setObjectField(callback) {
db.setObjectField(objectKey, 'reputation', 5, function(err, result) {
console.log('setObjectField return', result);
callback(err, {'setObjectField': result});
});
}
function getObjectField(callback) {
db.getObjectField(objectKey, 'age', function(err, age) {
console.log('getObjectField return', age);
callback(err, {'getObjectField' : age});
});
}
function getObjectFields(callback) {
db.getObjectFields(objectKey, ['name', 'lastname'], function(err, data) {
console.log('getObjectFields return', data);
callback(err, {'getObjectFields':data});
});
}
var tasks = [
//setObject,
getObject,
setObjectField,
getObject,
getObjectField,
getObjectFields,
];
require('async').series(tasks, function(err, results) {
if(err) {
console.log(err);
res.send(err.message);
} else {
res.json(results);
}
});
});
}); });
}; };

Loading…
Cancel
Save