Wednesday, 7 June 2017

node.js using mongodb library to insert json file - MongoError: error parsing element 0 of field documents

I am using node.js code with mongodb library to insert a json file in mongodb. However I am getting error:

/bigdata/node/node_modules/mongodb/lib/utils.js:123
    process.nextTick(function() { throw err; });
                                        ^
MongoError: error parsing element 0 of field documents :: caused by :: wrong type for '0' field, expected object, found 0: "{ Title: 'Chicago',

My node.js code is:

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var util = require("util");
var fs = require("fs");
var url = 'mongodb://USER:PASS@X.X.X.X:27017/test';
MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    console.log("Connected to Database");
        fs.readFile("doc.json", 'utf8',function (err,data) {
        var content = util.format(data);
        console.log(content);
        db.collection('Descarga').insert(content, function(err, records) {
                if (err) throw err;
                //console.log("Record added as " + records[0]._id);
                console.log("Record added");
                });
        });
});

Please help me to resolve this issue.

Thanks, Shilpa



via TextShilpa

No comments:

Post a Comment