Search code examples
mongodbstronglooploopback

StrongLoop Mongodb Models


I'm attempting to create a very simple loopback application that connects to a Mongo database.

To get started, I have created a mongo database using data from the mongo website that uses this dataset of restaurants.

How can I model this structure in a loopback application? All I can find is simple examples that do not contain nested structures. What am I missing?


Solution

  • You'll need following models:

    Restaurant

    • id (Type: Object, Autogenerated)
    • name (Type: String)
    • borough (Type: String)
    • cuisine (Type: String)
    • address (hasOne relation with an addressId field pointing to one address document (row) in collection (table))
    • grades (hasMany relation)

    RestaurantGrade

    • id (Type: Object, Autogenerated)
    • grade (Type: String)
    • score (Type: Number)
    • date (Type: Date)
    • restaurantId (belongsTo relation)

    Address

    • id (Type: Object, Autogenerated)
    • building (Type: String)
    • street (Type: String)
    • zipCode (Type: String)
    • coord (Type: GeoPoint)

    The normalization process of tables of relational database still applies when creating MongoDB collections. Check the following with your use cases before considering nested structure.

    Do yo need to show the nested (embedded) data structure as static representation of data or you want to user sort, search and limit on the fields of embedded objects?

    For example, if you want to see all restaurants for a zipcode, then you should not embed address in the Restaurant collection but instead separate it out in a different collection (as in above models).