How to Create MongoDB Collection from Json File Using Node

Viewed 107

i wish to create a mongodb collection from json string with a particular structure. no error messages. mongodb connection works. mongo collection gets created with default data (eg _id/ObjectId) but without content from json file. smth's wrong: how the model is defined and how the update method is defined.

Input.json

{
  "BYOB" : {
    "abc" : 1
    "def" : "c"
  },
  ...
}

Model.js

...
var modelSchema = new Schema({
  xyz: {
    abc: Number,
    def: String
  }
}
...

Update.js

...
const incoming = require('./Input.json');
var collectionModel = require('./Model');

module.exports.updateModel = function() {
  
    collectionModel.update(
      {},
      incoming,
      {upsert: true, multi: true}, function(err, result) {
        if (!err) {
          console.log("Collection is created: ", result);
        } else {
          console.log("Collection is not created: ", err);
        }
      }
    );
}
...
0 Answers
Related