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

前端 未结 4 1395
一个人的身影
一个人的身影 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:47

    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);
    }
    // ...
    
    0 讨论(0)
  • 2021-02-05 11:49

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-02-05 11:59

    I would also recommend looking into mongo-seeding. There is both a JS library version and a CLI version. The motivation for this library is described here.

    0 讨论(0)
提交回复
热议问题