Ola Karlsson
Ola Karlsson

Reputation: 151

Insert json from API-call into mongodb

I'm very fresh into both node.js and mongodb but I do really like it thus far. I'm trying to make an api-call and insert the data recieved into mongodb. Using cloud9 IDE btw.

On SO now I've only found posts about importing json files such as this one Insert json file into mongodb

var url = "API CALL HERE"

request(url, function(error, response, body){
     if(!error && response.statusCode == 200){
        var data = JSON.parse(body);
        res.send(data);

        var MongoClient = require('mongodb').MongoClient;
        var url = "mongodb://localhost:27017/mydb";

        MongoClient.connect(url, function(err, db) {
          if (err) throw err;
          var myobj = data;
          db.collection("dabas").insertMany(myobj, function(err, res) {
            if (err) throw err;
            console.log("Number of documents inserted: " + res.insertedCount);
            db.close();
          });
        });
     }
});

A sample from the json file looks like this:

// 20171013085454
// http://api.dabas.com/DABASService/V2/article/GTIN/07310100774460/json?apikey=2d2fbadc-dbe1-420f-a777-65912d65e388

{
  "Artikelbeskrivning": null,
  "Artikelegenskap": null,
  "Produktkod": "103511784459 / Kolonial/Speceri -- Kakao/Chokladdryck -- Kakaopulver -- Kakaopulver",
  "OvrigObligatoriskMarkning": null,
  "MaximalaAntaletMinstaEnheterIforpackningen": 0,
  "Hyllkantstext": "Kakaopulver 20-22",
  "Storlek": "1kg",
  "TullstatistisktNummer": null,

  "Varningsetiketter": [

  ],
  "Sasongskoder": [

  ],
  "Produktklasser": [

  ],
  "MaskinellMarkningar": [
    {
      "MaskinellMarkning": null,
      "TypAvMarkning": null,
      "Databarartyp": "EAN UPC"
    }
  ]}

When running I get this error message:

/home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb/lib/mongo_client.js:433
          throw err
          ^
MongoError: docs parameter must be an array of documents
    at Function.MongoError.create (/home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb-core/lib/error.js:31:11)
    at Collection.insertMany (/home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb/lib/collection.js:501:32)
    at /home/ubuntu/workspace/Projects/Mathubben/app.js:26:38
    at connectCallback (/home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb/lib/mongo_client.js:515:5)
    at /home/ubuntu/workspace/Projects/Mathubben/node_modules/mongodb/lib/mongo_client.js:430:11
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

Upvotes: 2

Views: 8003

Answers (3)

Anushka Ahir
Anushka Ahir

Reputation: 146

//modify your code like this

var url = "API CALL HERE"

request(url, function(error, response, body){

 if(!error && response.statusCode == 200){
    var data = JSON.parse(body);
    res.send(data);

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/mydb";

    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      var myobj = [];
      myobj.push(data);
      db.collection("dabas").insertMany(myobj, function(err, res) {
        if (err) throw err;
        console.log("Number of documents inserted: " + res.insertedCount);
        db.close();
      });
    });
 }

});

//your problem is you passing obj not array

//see below link for your reference

https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/

Upvotes: 1

ritesh sangani
ritesh sangani

Reputation: 340

The insertMany method accepts an array of documents

insertMany([ <doc 1> , <doc 2>, ... ],{
  writeConcern: <document>,
  ordered: <boolean>
  }
)

You are passing an object.

Solution 1: Assuming that you will receive a singe object for the body parameter, you can modify your code as below:

request(url, function(error, response, body){
 if(!error && response.statusCode == 200){
    var data = []
    var data.push(JSON.parse(body));
    res.send(data);

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/mydb";

    MongoClient.connect(url, function(err, db) {
      if (err) throw err;
      var myobj = data;
      db.collection("dabas").insertMany(myobj, function(err, res) {
        if (err) throw err;
        console.log("Number of documents inserted: " + res.insertedCount);
        db.close();
      });
    });
 }
});

Solution 2: You need to modify code to receive Json data in an array.

Upvotes: 1

Ola Karlsson
Ola Karlsson

Reputation: 151

It was the insertMany which was the issue

 db.collection("dabas").insertMany(myobj, function(err, res) {

when using

 db.collection("dabas").insert(myobj, function(err, res) {

It worked properly

Upvotes: 3

Related Questions