如何从JSON创建Mongoose模式 [英] How to create a Mongoose schema from JSON

查看:69
本文介绍了如何从JSON创建Mongoose模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mongodb,nodejs和mongooseJS的新手.最近,我一直在尝试为JSON创建猫鼬模式.

I am new into mongodb, nodejs and mongooseJS. Lately, I have been trying to create a mongoose schema for my 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"
}]
}

这是我的JSON文件.我尝试从 https://github.com/nijikokun/generate-schema 使用generate-schema给了我以下输出

This is my JSON file that I have. I tried using generate-schema from https://github.com/nijikokun/generate-schema which gave me the following output

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

我使用了它,当我从chrome网站商店使用Postman测试它时,我无法使用get请求从数据库中检索完整的JSON.我都无法成功运行发布请求.

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. Neither I was able to run a post request successfully.

最近我尝试使用JSON模式而不是使用

Recently I tried using JSON schema instead of the mongoose schema using

mongoose.Schema("JSON Schema')

当我尝试使用JSON Schema时,我能够使用GET请求从mongodb集合中检索数据,但无法使用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

我也在考虑删除nodejs并重新开发Java和mongodb中的Web服务.如果我尝试使用Java Web服务与mongodb进行交互,这会影响我的Web应用程序的性能吗?

I was also thinking about dropping nodejs and redeveloping the webservice in java and mongodb. If I try to use Java web service for interacting with mongodb, is it going to affect the performance of my web app?

推荐答案

您可以使用Generate Schemas模块执行此任务.

You can use Generate Schemas module to do this task.

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

console.log(JSON.stringify(schema))

由于您有两个主要属性,其中一个是endpoints 和其他poi

Since you have two main properties one is endpoints and other poi

这是您的JSON对象的输出架构

And here is the output schema of your JSON object

    {
  "$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

这篇关于如何从JSON创建Mongoose模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆