Was ist die beste Methode zum Seeding einer Node / MongoDB-Anwendung?

Also, ich bin neu im MEAN-Stack und habe versucht, MongoDB zu säen. Ich verwende Mongoose, um mit der Datenbank zu kommunizieren, und es gibt eine Reihe von Dokumentationen, die darauf hinweisen, dass ich in der Lage sein sollte, mit JSON-Dateien zu arbeiten.

Was ich versucht habe:

node-mongo-seed; Ziemlich einfach, wirft aber konsequent Fehler auf das Ende von Arrays. (Vielleicht ist das fehlende bson-Modul schuld?)

{ [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 {]

Mungo-Samen; Es ist auch ziemlich einfach und fügt die JSON-Objekte vor dem Export in die Datenbank in eine Variable ein. Vielversprechend, aber ... mehr Fehler ...

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 waren meine Gedanken, dass es wahrscheinlich ein Syntaxfehler in der JSON-Struktur war, aber das Herumspielen hat keine wirklichen Lösungen ergeben (oder ich vermisse es vielleicht?). Beispiel meines JSON:

{
    {
        "header": "Dan's 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's 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."
            }
        ]
    }
}

Zusätzlich bin ich mir nicht ganz sicher, wie Filialdokumente in JSON angegeben werden sollen (vorausgesetzt, ich kann den Seeding-Prozess überhaupt erst zum Funktionieren bringen).

Hier ist mein Modell:

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

Jeder Einblick in die Navigation dieser Themen wäre sehr dankbar. Vielen Dank

Antworten auf die Frage(6)

Ihre Antwort auf die Frage