简体   繁体   English

如何从JSON创建Mongoose模式

[英]How to create a Mongoose schema from JSON

I am new into mongodb, nodejs and mongooseJS. 我是mongodb,nodejs和mongooseJS的新手。 Lately, I have been trying to create a mongoose schema for my JSON. 最近,我一直在尝试为我的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"
}]
}

This is my JSON file that I have. 这是我的JSON文件。 I tried using generate-schema from https://github.com/nijikokun/generate-schema which gave me the following output 我尝试使用https://github.com/nijikokun/generate-schema中的 generate-schema,它给了我以下输出

 { 
endpoints:[ 'String' ], 
poi: [ 'String' ]
 }

I used this and when I tested it using Postman from chrome webstore, I was not able to retrieve the complete JSON from the database using the get request. 我使用了这个,当我使用Chrome webstore中的Postman测试它时,我无法使用get请求从数据库中检索完整的JSON。 Neither I was able to run a post request successfully. 我都没能成功运行发布请求。

Recently I tried using JSON schema instead of the mongoose schema using 最近我尝试使用JSON模式而不是使用mongoose模式

mongoose.Schema("JSON Schema')

When I try using JSON Schema I am able to retrieve the data from the mongodb collections using the GET request but I'm not able to POST data correctly with the JSON Schema 当我尝试使用JSON Schema时,我能够使用GET请求从mongodb集合中检索数据,但是我无法使用JSON Schema正确地发布数据

I was also thinking about dropping nodejs and redeveloping the webservice in java and mongodb. 我也在考虑删除nodejs并重新开发java和mongodb中的webservice。 If I try to use Java web service for interacting with mongodb, is it going to affect the performance of my web app? 如果我尝试使用Java Web服务与mongodb进行交互,是否会影响我的Web应用程序的性能?

You can use Generate Schemas module to do this task. 您可以使用“ Generate Schemas模块来执行此任务。

var jsonObject={
var GenerateSchema = require('generate-schema')
var schema = GenerateSchema.json('Product',jsonObject);

console.log(JSON.stringify(schema))

Since you have two main properties one is endpoints and other poi 由于您有两个主要属性,一个是endpoints和其他poi

And here is the output schema of your JSON object 这是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 所以我认为你应该根据你的对象创建自定义模式,这对你来说会更好

You can also get other references here 你也可以在这里获得其他参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM