4

I need to import a JSON file to a data base Mongoose. This should be a script that should be run daily.

I have my JSON as follows :

[
 {
   "grant_title": "name SRL",
   "id": "30579843531",
   "total_amount": "551954"
},{
   "grant_title": "AADI CAPIF ASOCIACION CIVIL",
   "id": "30574449967",
   "total_amount": "37050"
},{
   "grant_title": "LA CAMPAÑOLA S.A",
   "id": "23311183419",
   "total_amount": "139292"
  }
]

I need this information to be stored in a database .

This should be whether or if a script NodeJS (express). Please be clear , because I am a beginner .

2
  • WHY ARE YOU SHOUTING? Commented Aug 25, 2016 at 14:03
  • Sorry, my english is poor :( . I need help. :) Commented Aug 25, 2016 at 14:21

1 Answer 1

7

Let's take this gradually. First off you are going to need to either create a CRON job, or use a NodeJS scheduler such as agenda to execute your function at a time interval.

Now... inside your function you need to load your JSON file into NodeJS.

var json = require('/path/to/file.json');

Finally... use insertMany to do a bulk insert into your collection.

db.collectionName.insertMany(json, function(err,result) {
   if (err) {
     // handle error
   } else {
     // handle success
   }
});

EDIT: Here is an example of using Agenda in NodeJS to run a program daily at 11:30 AM

var Agenda = require('agenda');

startScheduler = function() {
    // Set the scheduler utilizing Agenda
    var agenda = new Agenda({db: {address: 'url/to/database'}});

    agenda.define('RUN AT 1130 AM', function(job, done) {
        // your function goes here...
        done();
    });

    agenda.on('ready', function() {
        // CRON SYNTAX
        // * * * * * *
        // Minute / Hour / Day / Month / Day of week (0-6) Sunday-Saturday
        agenda.every('30 11 * * *', 'RUN AT 1130 AM'); // Run daily at 11:30 AM

        agenda.start();
    });
};

startScheduler();
Sign up to request clarification or add additional context in comments.

2 Comments

After struggling a little bit with db.collectionName.insertMany(...) (error 'Cannot read property 'insertMany' of undefined'), I've ended up writing something equivalent to const mongoose = require('mongoose'); /* ... DB connexion ... */ mongoose.connection.db.collection('collectionName').insertMany(...). However my suggestion is a completely schema-less approach (possibly Ok for ex. if you aim to import data from a reliable export without bothering with schemas declarations) ; in most situations using Mongoose Schemas would be safer.
... furthermore if you use Mongoose Schemas, if you insert a date string in your JSON data, it will still have the type "Date" in Mongo (instead of being stored as a raw string). Same goes certainly for other data types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.