iPhone Developer iPhone Developer - 22 days ago 6
JSON Question

using mongodb for caching json responses

Disclaimer: I'm new to NoSQL databases, if something is not clear, appreciate comments and questions to clear things up.

I'm calling some 3rd party web services to get JSON response but need a way to cache the response to avoid repeated calling since each response is constant across remote entities.

Question.

I've selected mongodb, is it the right tool for the job, the response entities have lengthy schemas and mongoose is forcing me to define one, is there a way to avoid having to define schema and just save the response by some id and read it later. if someone can kindly help with condition for checking the A. cache and B. saving.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

if() { // A. condition to check if cached response is available

} else { // call web service
http.request({}, function (response) {
var buffers = [];
response.on('data', function (chunk) {
buffers.push(chunk)
});
response.on('end', function () {
var data = Buffer.concat(buffers).toString();
// B. how to save data to a mongoose db
});
})
}


If approach is not right, and I should be using something else then please also enlighten.

Answer

If you don't have a schema available then don't use mongoose. Just store the json as mongo documents directly.

https://docs.mongodb.com/getting-started/node/client/

https://docs.mongodb.com/getting-started/node/update/