我是 mongodb、nodejs 和 mongooseJS 的新手。最近,我一直在尝试为我的 JSON 创建 Mongoose 模式。
{
"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"
}]
}
这是我拥有的 JSON 文件。我尝试使用 https://github.com/nijikokun/generate-schema 的生成模式,它给了我以下输出
{
endpoints:[ 'String' ],
poi: [ 'String' ]
}
我使用了它,当我使用来自 chrome webstore 的 Postman 对其进行测试时,我无法使用 get 请求从数据库中检索完整的 JSON。我也无法成功运行发布请求。
最近我尝试使用 JSON 模式而不是 mongoose 模式使用
mongoose.Schema("JSON Schema')
当我尝试使用 JSON Schema 时,我能够使用 GET 请求从 mongodb 集合中检索数据,但我无法使用 JSON Schema 正确地发布数据
我也在考虑放弃 nodejs 并在 java 和 mongodb 中重新开发 web 服务。如果我尝试使用 Java 网络服务与 mongodb 交互,它会影响我的网络应用程序的性能吗?
最佳答案
您可以使用 Generate Schemas
模块来完成此任务。
var jsonObject={
var GenerateSchema = require('generate-schema')
var schema = GenerateSchema.json('Product',jsonObject);
console.log(JSON.stringify(schema))
因为你有两个主要属性,一个是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"
}
}
}
}
}
}
Suggestion: You will get some unwanted field and you have to modify it. So I think you should create custom schema on the basis of your object, which would be better for you
您还可以获得其他引用资料 here
关于java - 如何从 JSON 创建 Mongoose 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39362065/