【问题标题】:What is the best method to seeding a Node / MongoDB application?播种 Node / MongoDB 应用程序的最佳方法是什么?
【发布时间】:2016-07-01 12:28:48
【问题描述】:

所以,我是 MEAN 堆栈的新手,我在尝试播种 MongoDB 时碰壁了。我正在使用 Mongoose 与数据库进行通信,并且有一堆文档建议我应该能够使用填充的 JSON 文件进行播种。

我尝试过的:

node-mongo-seed;非常直截了当,但始终在数组末尾抛出错误。 (也许缺少的 bson 模块有问题?)

{ [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;也很简单,基本上在导出到数据库之前将 JSON 对象放入一个变量中。有希望,但是...更多错误...

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...

所以,我的想法是,这可能是 JSON 结构中的语法错误,但解决这个问题并没有产生任何真正的解决方案(或者我可能错过了它?)。我的 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."
            }
        ]
    }
}

此外,我不完全确定如何在 JSON 中指定子文档(假设我首先可以让播种过程正常工作)。

这是我的模型:

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

任何有关如何解决这些问题的见解将不胜感激。谢谢!

【问题讨论】:

  • 您也可以考虑Mongo Seeding,这是一种将数据导入数据库的灵活解决方案。这是我最近在做的一个开源项目,它使您能够使用 JS 库、CLI 和 Docker 映像来播种 MongoDB 数据库。它支持 JSON、JavaScript 和 TypeScript(Docker/自定义 TS 应用程序)来定义数据。

标签: json node.js mongodb mongoose


【解决方案1】:

您可以使用 mongoimport 在 CLI 中填充 MongoDB

它将一个 JSON 文件加载到指定的 MongoDB 实例和集合中,您只需要在执行之前运行一个 mongod 实例即可。

这是使用mongoimportwalkthrough

【讨论】:

  • 这绝对帮助我完成了我所期待的事情,至少部分实现了。我(还)不确定如何修改流程以包含子文档(很可能是mongodump),但这是朝着正确方向迈出的一大步。谢谢!
  • 你说的包含子文档是什么意思?
  • This post 对于mongoimport的子文档可能会对您有所帮助
【解决方案2】:

您的 JSON 没有流动您的架构。

将您的 JSON 修复为:

{
    {
        "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."
            }
        ]
    }
}

您可以使用 mongoose-data-seed 编写自己的种子脚本,与您的 mongoose 模型进行交互: https://github.com/sharvit/mongoose-data-seed

【讨论】:

    【解决方案3】:

    我在一个项目中解决了这个问题,方法是使用 mongoexport --jsonArray 将相关数据转储到扩展的 JSON 数组格式文件,然后使用 EJSON 包将其导入 Node 应用程序内的 POJO 格式。然后,我只需使用 Mongoose 使用您使用 Mongoose 创建的正确集合模型将生成的 JS 数组插入回数据库。

    首次运行应用程序所需的 JSON 数据文件已检入应用程序存储库。 下面是一个快速示例,您可以根据自己的目的进行调整:

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

    【讨论】:

      【解决方案4】:

      我还建议您查看mongo-seeding。既有 JS 库版本,也有 CLI 版本。这个库的动机描述为here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-05
        • 2011-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        • 1970-01-01
        相关资源
        最近更新 更多