Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor simple schema for mongo geo location data

I want to create an admin form for my meteor app; I was going to try Ogno Admin before resorting to building one from scratch but I'm not sure if it can support the data in the format that I need. My current app data goes into mongo like this:

Beaches.insert({
    "name": "Entry name",
    /* location stored like this so I can use mongo $near queries */
    "location": {
        "type": "Point",
        "coordinates": [-5.0990296,50.110757]
    },
    /* could be many images, minimum 1 */
    "images": [
        {
            "url": "image1.jpg",
            "caption": "Image caption"
        }
    ],
    "shortDesc": "A delightful description...",
    /* fixed list of attributes stored as objects */
    "attributes": {
        "attr 1": {
            "score": 2,
            "text": "attr1 text"
        },

Could I write a simple schema to support the different arrays/objects above (esp. location coords)? They have to be square bracket formatted [lng, lat] - and would ogno admin work with this, or would I have to write the custom admin stuff? It might be easier for me to build the admin site in something else and get that to output JSON data for Meteor.

Update with possible schema code

Beaches = new SimpleSchema({
  name: {
    type: String,
  },
  location: {
    type: [Object]
  },
    location.$.type: {
    /* how do I force '"type" : "Point" into every entry?
       use 'autovalue' with the .clean() function?*/
    },
      location.$.coordinates: {
      /* how do I ensure a [x,y] array in here? */
    },
  images: {
    type: [Object]
  },
    "images.$.url": {
        type: String
    },
    "images.$.caption": {
        type: String
    },
  attributes: {
    type: [Object]
  },
  /* note that my attributes above are all prefixed with a 'name'
     eg. "attr 1" : {}
     I'm not sure how to declare these either!
  */
  ...
});
like image 815
BellamyStudio Avatar asked Dec 02 '22 16:12

BellamyStudio


1 Answers

Hmm I dont know exactly the solution to your process of saving geocoords. But if you want to save lng and lat you have to pass a prefix. Why? Well geocoords have different validation ranges. Latitude is only available from -90 to 90 and Longitude from -180 to 180. If you dont save a prefix how do you want to ensure which is which coordinate? Another hint I have done false someday is to store the coords in longitude, latitude order.

The schema I am using looks like this:

GeocoordsSchema = new SimpleSchema({
  lng: {
    type : Number,
    decimal: true,
    min: -180,
    max: 180
  }, 
  lat: {
    type : Number,
    decimal: true,
    min: -90,
    max: 90
  }
});

Now you create nested schemas. Just extend GeocoordsSchema with LocationSchema and add a attribute.

LocationSchema = new SimpleSchema({
  type : {
    type : String,
    autoValue: function() {
      return "Point";
    }
  },
  coordinate: {
    type: GeocoordsSchema 
  }
});

If you want to have an array of LocationSchema than you can wrap the schema in [] brackets.

BeachesSchema = new SimpleSchema({
  loc: {
    type: [LocationSchema]
  }
});

I havent tested but thats the way how I create and nest different schemas. Well this solution needs an identifier of lat and lng. Why dont you want to prefix your data?

like image 118
chaosbohne Avatar answered Dec 04 '22 11:12

chaosbohne