所以,我是MEAN堆栈的新手,我试图播种MongoDB。我正在使用Mongoose与数据库进行通信,并且有大量文档表明我应该能够使用填充的JSON文件进行播种。
我尝试了什么:
node-mongo-seed ;非常简单,但在数组末尾始终抛出错误。 (也许缺少的bson模块有问题?)
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...
mongoose-seed ;同样非常简单,基本上在导出到数据库之前将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结构中的语法错误,但是玩弄它并没有产生任何真正的解决方案(或者我可能错过了它?)。我的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中指定子文档(假设我可以让播种过程首先正常工作)。
这是我的模特:
choco install python2
choco install visualstudioexpress2013windowsdesktop
非常感谢有关如何解决这些问题的任何见解。谢谢!
答案 0 :(得分:11)
您可以使用mongoimport
它会将JSON文件加载到指定的MongoDB实例&集合,您只需要在执行之前运行mongod
实例。
以下是使用mongoimport
的{{3}}。
答案 1 :(得分:1)
你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编写自己的种子脚本,以便与您的猫鼬模型进行交互: https://github.com/sharvit/mongoose-data-seed
答案 2 :(得分:0)
我在项目上解决了此问题,方法是使用 <soapenv:Body>
<bean:getPersonByUserIdIP>
<bean:attribute>name</bean:attribute>
<bean:attribute>address</bean:attribute>
<bean:userId>hx52301</bean:userId>
<bean:userType/>
</bean:getPersonByUserIdIP>
</soapenv:Body>`
将相关数据转储到扩展的JSON数组格式的文件中,然后使用mongoexport --jsonArray
包将其导入到Node应用程序中的POJO格式。然后,我仅使用Mongoose,使用您使用Mongoose创建的正确集合模型,将结果JS数组重新插入数据库中。
为应用程序播种以进行首次运行所需的JSON数据文件被检入到应用程序存储库中。 这是一个快速示例,您也许可以适应您的目的:
EJSON
答案 3 :(得分:0)
我还建议您研究mongo-seeding。有JS库版本和CLI版本。 here描述了该库的动机。