What is the best method to seeding a Node / MongoDB application? What is the best method to seeding a Node / MongoDB application? mongoose mongoose

What is the best method to seeding a Node / MongoDB application?


You can populate MongoDB in the CLI using mongoimport

It will load a JSON file into a specified MongoDB Instance & Collection, all you need is for a mongod instance to be running before executing.

Here is a walkthrough of using mongoimport.


You JSON is not flowing your schema.

Fix your JSON to this:

{    {        "name": "Dan's Place",        "rating": 3,        "address": "125 High Street, New York, 10001",        "coordinates": [-73.0812, 40.8732],        "attributes": ["Hot drinks", "Food", "Premium wifi"],        "openHours": [            {                "days": "Monday - Friday",                "opening": "7:00am",                "closing": "7:00pm",                "closed": false            },            {                "days": "Saturday",                "opening": "8:00am",                "closing": "5:00pm",                "closed": false            },            {                "days": "Sunday",                "closed": true            }        ],        "reviews": [            {                "rating": 4,                "author": "Philly B.",                "timestamp": "new Date('Feb 3, 2016')",                "body": "It was fine, but coffee was a bit dull. Nice atmosphere."            },            {                "rating": 3,                "author": "Tom B.",                "timestamp": "new Date('Feb 23, 2016')",                "body": "I asked for her number. She said no."            }        ]    },    {        "name": "Jared's Jive",        "rating": 5,        "address": "747 Fly Court, New York, 10001",        "coordinates": [-73.0812, 40.8732],        "attributes": ["Live Music", "Rooftop Bar", "2 Floors"],        "openHours": [            {                "days": "Monday - Friday",                "opening": "7:00am",                "closing": "7:00pm",                "closed": false            },            {                "days": "Saturday",                "opening": "8:00am",                "closing": "5:00pm",                "closed": false            },            {                "days": "Sunday",                "closed": true            }        ],        "reviews": [            {                "rating": 5,                "author": "Jacob G.",                "timestamp": "new Date('Feb 3, 2016')",                "body": "Whoa! The music here is wicked good. Definitely going again."            },            {                "rating": 4,                "author": "Tom B.",                "timestamp": "new Date('Feb 23, 2016')",                "body": "I asked to play her a tune. She said no."            }        ]    }}

You can use mongoose-data-seed to write your own seed script that interacting your mongoose models with:https://github.com/sharvit/mongoose-data-seed


I solved this issue on a project by dumping the relevant data to an extended JSON array formatted file using mongoexport --jsonArray, then importing this back into POJO format inside the Node application using the EJSON package. I then just use Mongoose to insert the resulting JS array back into the database using the correct collection model you've created using Mongoose.

The necessary JSON data files to seed the application for a first-run are checked into the application repository.Here's a quick sample you may be able to adapt to your purposes:

// ...// 'Items' is the Mongoose collection model.const itemResult = await Items.find({}).exec();if(itemResult.length === 0) {  const itemsSeedDataRaw = fs.readFileSync(`${__dirname}/data/items.json`, 'utf8');  const itemsSeedData = EJSON.parse(itemsSeedDataRaw);  await Items.insertMany(itemsSeedData);}// ...