Search code examples
javascriptmeteordatamodel

Data model for meteor


I have two collections, malls and shops. I want to keep location information of them (as latitude and longitude). For malls, it is easy, because they have only one location information, so i can define latitude and longitude property. But for shops, they can be either inside a mall or on the street, and they have multiple locations. I want to keep relation between shops and malls, for the places where they are inside a mall. I want to keep locations as a separate collection (at least for the shops), otherwise the data will be very heavy when i list the shops. Locations of a selected shop would be shown (downloaded) only when necessary. So question is, how should be the data model for locations collection? I'm considering something like this, but I don't feel it is the best idea. I consider to keep the lat, lan data of mall in its collection directly for simplicity (btw, i use simple schema):

ShopLocations = new Mongo.Collection('shoplocations');

var shopsLocationsSchema = new SimpleSchema ({
    name: {
        type: String,
        label: 'Location Name',
        max: 200
    },

    inMall: {
        type: Boolean,
        label: 'Is it in the mall?',
        optional: true
    },

    mallId: {
        type: String,
        label: 'Mall ID',
        optional: true
    },

    latitude: {
        type: Number,
        label: 'Latitude',
        decimal: true,
        optional: true      
    },

    longitude: {
        type: Number,
        label: 'Latitude',
        decimal: true,
        optional: true      
    }
});

ShopLocations.attachSchema(shopsLocationsSchema, {replace: true});

Solution

  • I think you have three collections: Malls, Shops, and Locations.

    Malls and Shops can both have locations, even if a mall only has one.

    Locations correspond either to a shop, a mall, or a shop in a mall.

    I would model a location simply as storeId, mallId, latitude, longitude then use the abscence of storeId to denote that the location only belongs to a mall. A location document that corresponds to a store would have at least storeId but would only have mallId if that location was inside a mall.

    The nice thing about MongoDB is that you can query for the existence or non-existence of keys as well as their values. For example:

    Find the locations of all malls:

    Locations.find({mallId: {$exists: true}, storeId: {$exists: false}});
    

    Find the locations of all stores not in malls:

    Locations.find({mallId: {$exists: false}, storeId: {$exists: true}});
    

    Find the locations of all stores in malls:

    Locations.(mallId: {$exists: true}, storeId: {$exists: true}});