Corey Bush
Corey Bush

Reputation: 183

MongoDB database design for users and their data

New to MongoDB and databases in general. I'm trying to make a basic property app with Express and MongoDB for practice.

I'm looking for some help on the best way to scheme this out.

Basically, my app would have landlords and tenants. Each landlord would have a bunch of properties that information is stored about. Things like lease terms, tenant name, maintenance requests, images, etc.

The tenants would be able to sign up and be associated with the property they live in. They could submit maintenance forms, etc.

Is this a good approach? Should everything be kept in the same collection? Thanks.

{
  "_id": "507f1f77bcf86cd799439011",
  "user": "Corey",
  "password": "hashed#PASSWORD",
  "email": "[email protected]",
  "role": "landlord",
  "properties": [
    {
      "addressId": "1",
      "address": "101 Main Street",
      "tenant": "John Smith",
      "leaseDate": "04/21/2016",
      "notes": "These are my notes about this property.",
      "images": [ "http://www.imagelink.com/image1", "http://www.imagelink.com/image2", "http://www.imagelink.com/image3"]
    },
    {
      "addressId": "2",
      "address": "105 Maple Street",
      "tenant": "John Jones",
      "leaseDate": "01/01/2018",
      "notes": "These are my notes about 105 Maple Ave property.",
      "images": ["http://www.imagelink.com/image1", "http://www.imagelink.com/image2", "http://www.imagelink.com/image3"],
      "forms": [
        {
          "formType": "lease",
          "leaseTerm": "12 months",
          "leaseName": "John Jones",
          "leaseDate": "01/01/2018"
        },
        {
          "formtype": "maintenance",
          "maintenanceNotes": "Need furnace looked at. Doesn't heat properly.",
          "maintenanceName": "John Jones",
          "maintenanceDate": "01/04/2018",
          "status": "resolved"
        },
      ]
    },
    {
      "addressId": "3",
      "address": "110 Chestnut Street",
      "tenant": "John Brown",
      "leaseDate": "07/28/2014",
      "notes": "These are some notes about 110 Chestnut Ave property.",
      "images": [ "http://www.imagelink.com/image1", "http://www.imagelink.com/image2", "http://www.imagelink.com/image3"]
    }
  ]
}

{
  "_id": "507f1f77bcf86cd799439012",
  "user": "John",
  "password": "hashed#PASSWORD",
  "email": "[email protected]",
  "role": "tenant",
  "address": "2",
  "images": [ "http://www.imagelink.com/image1", "http://www.imagelink.com/image2" ]
}

Upvotes: 0

Views: 59

Answers (1)

Nutatello
Nutatello

Reputation: 89

For this relation I'd suggest three collections (Landlords, Properties, and Tenants), with each tenant having a "landLordId" and "propertyId".

This "landLordId" would simply be the ObjectId of the landLord, and same for the property Id.

This will make your life easier if you plan to do any kind of roll-up reports or if the you have more than one-to-one mappings for landlords to properties or landlords to tenants. (Example, more than one property manager for a given property)

This just makes everything easier/more intuitive as you could simply add things like maintenance requests, lease terms etc in arrays on the tenants with references to whatever need be.

This offers the most flexibility in terms of being able to aggregate easily for any kind of report/query.

Upvotes: 1

Related Questions