What is the best method for planting a Node / MongoDB application?

So I'm new to the MEAN stack, and I hit the wall trying to sow MongoDB. I use Mongoose to communicate with the database, and there is a bunch of documentation out there suggesting that I should use samples using populated JSON files.

What I tried:

node-mongo-seed ; Pretty straight forward, but consistently throws errors at the end of the arrays. (Perhaps the missing bson module is faulty?)

{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson: Failed to load c++ bson extension, using pure JS version Seeding files from directory /Users/Antwisted/code/wdi/MEAN/seeds ---------------------- Seeding collection locations err = [SyntaxError: /Users/Antwisted/code/wdi/MEAN/seeds/locations.json: Unexpected token {] 

mongoose-seed ; Also pretty straightforward, basically puts JSON objects in a variable before exporting to the database. Promising, but ... more mistakes ...

 Successfully initialized mongoose-seed [ 'app/models/locationsModel.js' ] Locations collection cleared Error creating document [0] of Location model Error: Location validation failed Error creating document [1] of Location model Error: Location validation failed Error creating document [2] of Location model Error: Location validation failed... 

So, my thoughts were that this was probably a syntax error in the JSON structure, but playing with it did not give any real solutions (or maybe I miss it?). Sample of my JSON:

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

Also, I'm not quite sure how to specify subdocuments in JSON (assuming that I can get the sowing process to work correctly in the first place).

Here is my model:

 var mongoose = require('mongoose'); var subHoursSchema = new mongoose.Schema({ days: {type: String, required: true}, opening: String, closing: String, closed: {type: Boolean, required: true} }); var subReviewsSchema = new mongoose.Schema({ rating: {type: Number, required: true, min: 0, max: 5}, author: String, timestamp: {type: Date, "default": Date.now}, body: String }); var locationSchema = new mongoose.Schema({ name: {type: String, required: true}, address: String, rating: {type: Number, "default": 0, min: 0, max: 5}, attributes: [String], coordinates: {type: [Number], index: '2dsphere'}, openHours: [subHoursSchema], reviews: [subReviewsSchema] }); mongoose.model('Location', locationSchema); 

Any understanding of how to navigate these issues would be very helpful. Thanks!

+13
json mongodb mongoose
source share
4 answers

You can populate MongoDB in the CLI using mongoimport

It will load the JSON file into the specified MongoDB instance and the collection, all you need to do is start the mongod instance before executing.

Here is a walkthrough using mongoimport .

+11
source share

You JSON is not flowing in your schema.

Fix your JSON:

 { { "name": "Dan 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 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 the mongoose-data seed to write your own seed script that interacts with your mongoose models: https://github.com/sharvit/mongoose-data-seed

+1
source share

I solved this problem in the project by placing the corresponding data in an extended file in the JSON array format using mongoexport --jsonArray , and then importing it back into the POJO format inside the Node application with EJSON package. Then I just use Mongoose to insert the resulting JS array back into the database using the correct collection model created using Mongoose.

The necessary JSON data files to fill the application for the first run are checked in the application store. Here is a quick example that you can adapt to your goals:

 // ... // '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); } // ... 
0
source share

Hi, you better use Seedgoose . This seeder tool supports CLI and smart links. You can easily customize links for different models.

0
source share

All Articles