I'm trying to develop a synchronization server (think: SVN like) that accepts one or more documents (JSON string) from the client in one request (JSON stringified array of JS objects), inserts/updates them into mongodb and sends one response - which is a JSON string containing the insert/update status (and some more info like _id in mongo) of each document.
If it was one document, i could have done one insert/update and in its callback, i could have sent the response.
collection.insert(_data, function(error, result) {
if(error) res.send('error');
else res.send(JSON.stringify({result: result}));
});
But how to do this when i have more than one document. I could do insert/update one document in the previous one's callback. But i fear i'll end up with a dreadful ladder of code if i do that (i could do it in one function and recurse, yes).
Any help will be greatly appreciated. BTW, i'm using this driver: http://mongodb.github.io/node-mongodb-native/
Note: I'm not looking at batch inserts or updates, as each document that is being processed needs individual processing. Some may need to be inserted, some updated, and then there is version number & sync-status checking etc.