How to add geojson to objects

This is my first meteor project and I’m new to mongo (and document databases). I am writing a building inventory and therefore have a collection called Buildings. I will likely have about 200 buildings in the inventory.

I would like to put all of the buildings on a map (likely going to use leaflet). I want to colorize the buildings based on category info, so I’m using the following schema for my building objects

 'name': 'Big Building',
 'address': '123 my street',
 'location': {
    'type': 'Point',
    'coordinates': [ -84, 12 ], // WGS84 coordinates 
 'image': '...', // base64 representation of image
 'category 1': 'some value',
 'category 2': 'some other value'

There are potentially many categories (i.e. 15 or so). Is this the best way to store my building information? Can anyone offer me some advice on this schema?


1 Like

My understanding from your description is that category is a set.

I would put the categories in an object or an array

An object is a little bit easier to manage but you will have some redundancy because JavaScript gives you maps, not sets, thereafter you need to store a value there, say true

categories : { 
   one : true,
   two : true

In mongo you can query whether an object contains a given field with $exists

You can also use an array because Mongo gives you a way to treat arrays as sets whenever you update them which allows you to avoid duplicates

categories : ['one', 'two']

Mongo provides $addToSet that adds an element to an array if it’s not already there

Choosing between one or the other is a matter of taste and how you use them in the client and server code. For instance Blaze {{#each v in values}} can only iterate over arrays. If you choose an object representation, you will have to transform them into arrays just before sending them to Blaze. On the other hand if you need to test very often whether a category is present or not, with an object you write if(object[field]) which is constant time O(1) while an array representation requires (array.indexOf(field) >= 0) which is linear O(n)

My advice is pick one, complete a first version of your application, and then try the alternate.

I would like to do it correctly the first time, so that it serves every need :yum:

But seriously, your feedback is great and definitely food for thought. Thank you for the thoughtful response.