dwigt dwigt - 4 months ago 16
JSON Question

Inserting array of data into database using Node.js

As a rookie in web development I was hoping you can point me in the right direction on how to insert JSON data into a mongo database, to be used for automatic graph creation.

I have a JSON response from this URL converted to a JavaScipt object looking like this:

[ { game:
{ name: 'Counter-Strike: Global Offensive',
popularity: 244757,
_id: 32399,
giantbomb_id: 36113,
box: [Object],
logo: [Object],
_links: {} },
viewers: 246950,
channels: 488 },
{ game:
{ name: 'League of Legends',
...


However I wonder how I can structure this data in a database so I can later graph my data according to the time and date it was fetched.

I figure that I would want to keep a list of Ids related to popularity and date.

Can you point me in a direction on how to read a JSON array and update a Mongo collection with the data so it looks something like this?

{
_id: "32399",
name: "Counter-Strike: Global Offensive",
datapoints: [
{
2016-05-18T16:00:00Z
viewers: 246950
channels: 488
},
{
2016-04-18T16:00:00Z
viewers: 230000
channels: 433
}
]
}


I guess can iterate over every value using forEach. But is this the right method? and how do I sort the information? do I iteratively go through every entity and find viewer and channel values and save to a new document cache or use updateOne to update my database?

games.top.forEach(function(value) {
console.log(value)
});

Answer

Well, to start with you'd need to create a new Schema to determine the desired structure. Something along the lines of the following, although i'd advise reviewing the mongoose documentation before hand. http://mongoosejs.com/docs/schematypes.html

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var Game = new Schema({
    name: {
        type: String,
        required: true
    },
    datapoints: [{
        created: {
            type: Date,
            default: Date.now
        },
        viewers: {
            type: Number,
            default: 0
        },
        channels: {
            type: Number,
            default: 0
        }
    }]
});

module.exports = mongoose.model('Game', Game);

Then you'd simply need to create and save a new 'Game' instance for each row in the data.

var Game = require('your game model');

for(var idx = 0; idx < data.length; idx++) {
    var object = data[idx];

    var temp = new Game({
        name: object.name,
        datapoints: {
            created: Date.now(),
            viewers: object.viewers,
            channels: object.channels
        }
    });

    temp.save(function(error) {
        if(error) {
            console.log(error);
        }

        console.log('successfully saved');
    });
}

Please note, I haven't tested any of the above, it's straight from memory, so I'd very much suggest reading through the Mongoose documentation and perhaps following some tutorials to help familiarise yourself with the syntax and how Node and Mongoose work.

I hope this helps.

Comments