Json 为Node/MongoDB应用程序设定种子的最佳方法是什么?
所以,我对MEAN stack是新手,我在尝试播种MongoDB时遇到了困难。我正在使用Mongoose与数据库进行通信,有大量文档建议我应该能够使用填充的JSON文件进行种子设定 我所尝试的: ;非常直截了当,但始终在数组末尾抛出错误。(可能丢失的bson模块有故障?) ;也很简单,基本上是在导出到数据库之前将JSON对象放入变量中。有希望,但是。。。更多错误Json 为Node/MongoDB应用程序设定种子的最佳方法是什么?,json,node.js,mongodb,mongoose,Json,Node.js,Mongodb,Mongoose,所以,我对MEAN stack是新手,我在尝试播种MongoDB时遇到了困难。我正在使用Mongoose与数据库进行通信,有大量文档建议我应该能够使用填充的JSON文件进行种子设定 我所尝试的: ;非常直截了当,但始终在数组末尾抛出错误。(可能丢失的bson模块有故障?) ;也很简单,基本上是在导出到数据库之前将JSON对象放入变量中。有希望,但是。。。更多错误 Successfully initialized mongoose-seed [ 'app/models/locationsModel
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);
任何关于如何处理这些问题的见解都将不胜感激。谢谢 您可以使用 它将把一个JSON文件加载到指定的MongoDB实例和集合中,您所需要的只是在执行之前运行一个
mongod
实例
下面是一个使用
mongoimport
的示例,您的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数据种子编写自己的种子脚本,用于与mongoose模型交互:
我在一个项目中解决了这个问题,方法是使用
mongoexport--jsonArray
将相关数据转储到扩展的JSON数组格式文件中,然后使用EJSON
包将其导入节点应用程序中的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);
}
// ...
我还建议您调查一下。有JS库版本和CLI版本。创建这个库的动机如下所述。这无疑帮助我实现了我所期望的目标,至少在一定程度上是这样。我还不确定如何修改流程以包含子文档(很可能是
mongodump
),但这是朝着正确方向迈出的一大步。非常感谢。你说包含子文档是什么意思?您可以考虑使用“代码> ManguPux</代码>的子文档,也可以考虑将数据导入数据库的灵活解决方案。这是我最近正在进行的一个开源项目,它使您能够使用JS库、CLI和Docker映像为MongoDB数据库添加种子。它支持JSON、JavaScript和TypeScript(Docker/custom TS apps)来定义数据。
// ...
// '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);
}
// ...