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

前端 未结 4 1397
一个人的身影
一个人的身影 2021-02-05 11:15

So, I\'m new to the MEAN stack, and I\'ve hit a wall trying to seed MongoDB. I\'m using Mongoose to communicate with the database, and there\'s a bunch of documentation suggesti

4条回答
  •  盖世英雄少女心
    2021-02-05 11:51

    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

提交回复
热议问题