Home:ALL Converter>How to Create MongoDB Collection from Json File Using Node

How to Create MongoDB Collection from Json File Using Node

Ask Time:2022-06-29T23:25:24         Author:femtowatts

Json Formatter

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);
        }
      }
    );
}
...

Author:femtowatts,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/72804025/how-to-create-mongodb-collection-from-json-file-using-node
yy