persistent-poltergeist persistent-poltergeist - 3 months ago 39
Node.js Question

How to create a Mongoose schema from 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"
}]
}


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' ]
}


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.

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

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

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?

Answer

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))

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

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 will better for you

You can also get other reference here