per*_*ist 5 java mongodb node.js mongoose-schema
我是mongodb,nodejs和mongooseJS的新手。最近,我一直在尝试为JSON创建猫鼬模式。
{
"endpoints":["a","z"],
"poi":[{
"location_name": "a",
"latitude": " 10.1075702",
"longitude": "76.345662",
"distance" : "0.0"
}, {
"location_name": "b",
"latitude": "10.110199",
"longitude": "76.3489361",
"distance" : "2.0"
}, {
"location_name": "c",
"latitude": "10.1197471",
"longitude": "76.342873",
"distance" : "3.1"
}, {
"location_name": "d",
"latitude": "10.1254479",
"longitude": "76.3332626",
"distance" : "4.4"
}, {
"location_name": "e",
"latitude": "10.1443277",
"longitude": "76.2566017",
"distance" : "13.9"
}, {
"location_name": "f",
"latitude": "10.1487145",
"longitude": "76.2441114",
"distance" : "15"
}, {
"location_name": "z",
"latitude": "10.145578",
"longitude": "76.2317077",
"distance" : "16.9"
}]
}
Run Code Online (Sandbox Code Playgroud)
这是我的JSON文件。我尝试从https://github.com/nijikokun/generate-schema使用generate-schema,这给了我以下输出
{
endpoints:[ 'String' ],
poi: [ 'String' ]
}
Run Code Online (Sandbox Code Playgroud)
我使用了它,当我使用Chrome Webstore的Postman测试它时,我无法使用get请求从数据库中检索完整的JSON。我都无法成功运行发布请求。
最近我尝试使用JSON模式而不是使用猫鼬模式
mongoose.Schema("JSON Schema')
Run Code Online (Sandbox Code Playgroud)
当我尝试使用JSON Schema时,我能够使用GET请求从mongodb集合中检索数据,但无法使用JSON Schema正确地发布数据
我也在考虑删除nodejs并重新开发Java和mongodb中的Web服务。如果我尝试使用Java Web服务与mongodb进行交互,是否会影响Web应用程序的性能?
您可以使用Generate Schemas模块来完成此任务。
var jsonObject={
var GenerateSchema = require('generate-schema')
var schema = GenerateSchema.json('Product',jsonObject);
console.log(JSON.stringify(schema))
Run Code Online (Sandbox Code Playgroud)
由于您有两个主要属性,一个是endpoints
,另一个poi
这是您的 JSON 对象的输出模式
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"type": "object",
"properties": {
"endpoints": {
"type": "array",
"items": {
"type": "string"
}
},
"poi": {
"type": "array",
"items": {
"type": "object",
"properties": {
"location_name": {
"type": "string"
},
"latitude": {
"type": "string"
},
"longitude": {
"type": "string"
},
"distance": {
"type": "string"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
建议:你会得到一些不需要的字段,你必须修改它。所以我认为你应该根据你的对象创建自定义模式,这对你更好
您还可以在此处获得其他参考资料