Simultaneous reads from Mongo using mongoose - node.js

I have a situation where I have to gather data from three different collections and bundle it together ( there are no joins to be done here) and send it to the client.
Although I have what I think is an elegant solution, I want to ask the community whether there is a cleaner way of performing this operation ?
I am using mongoose as a data modeling layer:
async.parallel([
function(callback) {
ForecastModel.find({facebookId: req.query.playerId}, function(err, result) {
callback(err, result);
});
},
function(callback) {
ScoreModel.find({facebookId: req.query.playerId}, function(err, result) {
callback(err, result);
});
},
function(callback) {
FollowModel.findOne({facebookId: req.query.playerId}, function(err, result) {
callback(err, result);
});
}
],
function(err, results) {
var playerInfo = {};
playerInfo.forecasts = results[0];
playerInfo.score = results[1][0];
playerInfo.followModel = results[2];
res.send(playerInfo);
});

Related

mongodb native driver error on query

I am writing the filter using mongodb native driver, but it's driving me this error when you run the query.
In the case of this driver, it has no exec?
What is another way to perform this query?
exports.findAll = function(req, res) {
MongoClient.connect(url, function(err, db) {
var section = req.params.section;
var collection = db.collection(section);
var filter = req.query.filter ? {nameToLower: new RegExp('^' + req.query.filter.toLowerCase())} : {};
var query = collection.find(filter);
var count = 0;
collection.count(filter, function (error, result) {
count = result;
});
if(req.query.order) {
query.sort(req.query.order);
}
if(req.query.limit) {
query.limit(req.query.limit);
if(req.query.page) {
query.skip(req.query.limit * --req.query.page);
}
}
query.exec(function (error, results) {
res.json({
count: count,
data: results
});
});
});
};
Error:
TypeError: undefined is not a function
Better to use the async library in this case as it simplifies the code. In the case where you need to run multiple tasks that depend on each other and when they all finish do something else, use the
async.series() module. The following demonstrates how you can go about this in your case:
exports.findAll = function(req, res) {
var locals = {},
section = req.params.section,
filter = !!req.query.filter ? {nameToLower: new RegExp('^' + req.query.filter.toLowerCase())} : {};
async.series([
// Connect to DB
function(callback) {
MongoClient.connect(url, function(err, db) {
if (err) return callback(err);
locals.collection = db.collection(section); //Set the collection here, so the next task can access it
callback();
});
},
// Get count
function(callback) {
locals.collection.count(filter, function (err, result){
if (err) return callback(err);
locals.count = result; //Set the count here
callback();
});
},
// Query collection
function(callback) {
var cursor = locals.collection.find(filter);
if(req.query.order) {
cursor = cursor.sort(req.query.order);
}
if(req.query.limit) {
cursor = cursor.limit(req.query.limit);
if(req.query.page) {
cursor = cursor.skip(req.query.limit * --req.query.page);
}
}
cursor.toArray(function(err, docs) {
if (err) return callback(err);
locals.docs = docs;
callback();
});
}
], function(err) { //This function gets called after the three tasks have called their "task callbacks"
if (err) return next(err);
// Here locals will be populated with 'count' and 'docs'
res.json({
count: locals.count,
data: locals.docs
});
res.render('user-profile', locals);
});
};

mongojs not doing CRUD operations

var connection_string = 'localdb'; //local db by default
var mongojs = require('mongojs');
var collections = ['projects', 'homeAnimationData'];
var db = mongojs(connection_string, collections);
db.homeAnimationData.save({image: '/imagea', width: '200px'}, function(err, saved) {
if( err || !saved ) console.log('not saved');
else console.log('Saved');
});
db.homeAnimationData.find({}).limit(10).forEach(function(err, doc) {
if (err) throw err;
if (doc) {
console.dir(doc);
}
});
None of the consoles are working in nodejs app and I'm not getting any error?
I supect that you are actually calling db.close() but not telling us about it in your question. That, and the case of not seeing the output when you go to .find() data is primarily a problem of the fact that the methods here are 'asynchronous`, and you need to "wait" for the callback execution that you want.
Also the .forEach() method here should be used "sparingly", as it is almost never what you really want in production code. Here is the same thing that works fine with a little async flow control help from the "async" library:
var localdb = 'localdb',
async = require('async'),
mongojs = require('mongojs'),
collections = ['projects','homeAnimationData'],
db = mongojs(localdb,collections);
async.series(
[
function(callback) {
db.homeAnimationData.remove({},callback);
},
function(callback) {
var data = { "image": "/imagea", "width": "200px" };
db.homeAnimationData.save(data,function(err,saved) {
if (err) callback(err);
console.log('Saved');
callback();
});
},
function(callback) {
db.homeAnimationData.find({}).limit(10).toArray(function(err,docs) {
if (err) callback(err);
console.dir(docs);
callback();
});
}
],
function(err) {
if (err) throw err;
db.close();
}
);
Or alternately to calling the .toArray() method, you can use the "stream" interface, which is natively provided:
var localdb = 'localdb',
async = require('async'),
mongojs = require('mongojs'),
collections = ['projects','homeAnimationData'],
db = mongojs(localdb,collections);
async.series(
[
function(callback) {
db.homeAnimationData.remove({},callback);
},
function(callback) {
var data = { "image": "/imagea", "width": "200px" };
db.homeAnimationData.save(data,function(err,saved) {
if (err) callback(err);
console.log('Saved');
callback();
});
},
function(callback) {
var cursor = db.homeAnimationData.find({}).limit(10);
cursor.on('data',function(data) {
cursor.pause();
console.dir(data);
cursor.resume();
});
cursor.on('err',callback);
cursor.on('end',callback);
}
],
function(err) {
if (err) throw err;
db.close();
}
);
In the latter case, this gives you a lot better handling for larger results. The mongojs documtation itself provides and example of .pipe() for a JSONStream.
But everything works fine, you just have to remember this is "async" programming, and logically wait for execution to complete before moving on to some like "reading" the written data back, or indeed closing the database connection.

Call same function many times and process combined result set

I have a requirement to make several API requests and then do some processing on the combines result sets. In the example below, you can see that 3 requests are made (to /create) by duplicating the same request code however I would like to be able to specify how many to make. For example, I may wish to run the same API call 50 times.
How can I make n calls without duplicating the API call function n times?
async.parallel([
function(callback){
request.post('http://localhost:3000/create')
.send(conf)
.end(function (err, res) {
if (err) {
callback(err, null);
}
callback(null, res.body.id);
});
},
function(callback){
request.post('http://localhost:3000/create')
.send(conf)
.end(function (err, res) {
if (err) {
callback(err, null);
}
callback(null, res.body.id);
});
},
function(callback){
request.post('http://localhost:3000/api/store/create')
.send(conf)
.end(function (err, res) {
if (err) {
callback(err, null);
}
callback(null, res.body.id);
});
}
],
function(err, results){
if (err) {
console.log(err);
}
// do stuff with results
});
First, wrap the code that you want to call many times in a function:
var doRequest = function (callback) {
request.post('http://localhost:3000/create')
.send(conf)
.end(function (err, res) {
if (err) {
callback(err);
}
callback(null, res.body.id);
});
}
Then, use the async.times function:
async.times(50, function (n, next) {
doRequest(function (err, result) {
next(err, result);
});
}, function (error, results) {
// do something with your results
}
Create an array with as many references to the function as you need tasks in your workload. Then pass them to async.parallel. For example:
var async = require("async");
var slowone = function (callback) {
setTimeout(function () {
callback(null, 1);
}, 1000);
};
async.parallel(
dd(slowone, 100),
function (err, r) {
console.log(JSON.stringify(r));
}
);
// Returns an array with count instances of value.
function dd(value, count) {
var result = [];
for (var i=0; i<count; i++) {
result.push(value);
}
return result;
}
Note again that there is only one instance of the slow running function, in spite of there being many references to it.

MongoJS synchronous call

I'm fixing some bugs in one project. I need synchronous cursor, to load data in loop.
I have code:
var mongo = require('mongojs'),
dataMaps = [];
mongo.penguin_friends.find({ user_id_1: id_user }, function(err, friends) {
friends.forEach(function(data) {
var cursor = mongo.penguin_user_maps3.find({ user_id: data.id_user }, { fields: { maps: 1 } });
//I need to do dataMaps.push(cursor.data);
});
console.log("processingThisSomething()");
processSomething();
});
I need to complete this request before calling processSomething(); So I need to process mongodb query inside a loop synchronously.
It's not possible to make the queries synchronous as the API doesn't support it.
You'll have to provide a callback to .find() or a cursor method to receive the results:
cursor.toArray(function (err, maps) {
if (!err) {
dataMaps.push(maps);
}
});
But, you can replace the iterator with one that's asynchronous-aware, such as async.each(), to continue when they've completed:
async.each(
friends,
function (data, callback) {
var cursor = mongo....;
cursor.toArray(function (err, maps) {
if (!err) {
dataMaps.push(maps);
}
callback(err);
});
},
function (err) {
if (!err) {
processSomething();
}
}
);

How to save datetime as BSON Date using Node driver?

I'm using the Node MongoDB driver to collect analytics for my website and send them to MongoLab. How does one insert the current datetime into a document so it can later be used to do aggregation in Mongo?
To be clear, here is the code:
var query = {};
query['date_time'] = new Date();
Db.connect(mongoUri, function (err, db) {
if (!err) {
db.collection(analyticsCollection, function (err, coll) {
if (!err) {
coll.save(query, {safe:true}, function (err, result) {
db.close();
res.send(200,result);
//console.log(result);
});
} else {
res.json(503,{errno:503,msg:'error: collection does not exist.'});
}
});
} else {
res.json(503,{errno:503,msg:'error: database does not exist.'});
}
});
The MongoDB documentation describes how to create a BSON date in the mongo shell, but not in the Node driver and I can't find it in the Node driver docs either.
I found a way to do what I wanted to do. This seems kind of klugey to me, so if anyone can find a better way, please let me know. My solution uses the underscore.js library:
var _ = require('underscore');
vary query = {}
query['foo'] = 'bar'
Db.connect(mongoUri, function (err, db) {
if (!err) {
db.collection(analyticsCollection, function (err, coll) {
if (!err) {
coll.save(_.extend(query,{"datetime":new Date()}), {safe:true}, function (err, result) {
db.close();
res.send(200,result);
//console.log(result);
});
} else {
res.json(503,{errno:503,msg:'error: collection does not exist.'});
}
});
} else {
res.json(503,{errno:503,msg:'error: database does not exist.'});
}
});
Now I can do the aggregation (without needing to use the $project operator):
> db.analytics.aggregate([{$group:{_id:{day:{$dayOfMonth:"$datetime"}}}}])
{ "result" : [ { "_id" : { "day" : 15 } } ], "ok" : 1 }
Hope this helps someone.

Resources