Skip to content

MongoDB Schema

Kai Xu edited this page Dec 14, 2017 · 7 revisions

New Schema

General pattern

key1: string (or number),
key2: string (or number),
... (other keys)
info: json (everything else)

The fields needed to identify a record/document will be defined explicitly (key1, key2, ...), everything else goes into ‘info’ json object, which can change over time and be as detailed as we want.

Example

'user'

id: string (or number)
email: string
info: json

‘id’ and ‘email’ can be the two keys (used to identify user), and all the other information from google+ (name, image, etc.) goes into ‘Info’.

'session'

id: string (or number),
... (any other key(s) used to identify a session)
info: json

'action' or 'node'

id: string (or number)
... (any other key(s) used to identify an action or node)
info: json

Overall schema

{
id: string (or number)
email: string
info: json // other user information

sessions: [
  {
    id: session1
    info: json // other session information
    nodes: [
      {
        id: node1
        info: json // other node information
      }
      {
        id: node2
        info: json
      }
      ...
    ]
  }

  {
    id: session2
    info: json
    nodes: [
      ...
    ]
  }

  ...
]
}

Old Schema

Fields to Record:

UserRecord:

  1. UserID
  2. Email Address
  3. CreatedOn

SessionRecord:

  1. SessionID
  2. CreatedOn

ActionRecord:

  1. Tab ID
  2. URL
  3. Type
  4. Time
  5. EndTime
  6. Favicon
  7. Image
  8. Seen

Proposed Schema:

{
   _id: UserID
   fullname: UserName, 
   email: EmailAddress,
   createdOn: AccountCreateDate,
   sessions: [	
	  {
		 sid: SessionId,
		 dateCreated: DATE_TIME,
		 tabid: TabID,
		 url: TabUrl,
		 type: TabType,
		 startTime: StartTime,
		 endTime: EndTime,
		 favicon: FaviconURL,
		 image: FeaturedImage,
		 seen: SeenRecord
		 
	  },
	  {
		 sid: SessionId,
		 dateCreated: DATE_TIME,
		 tabid: TabID,
		 url: TabUrl,
		 type: TabType,
		 startTime: StartTime,
		 endTime: EndTime,
		 favicon: FaviconURL,
		 image: FeaturedImage,
		 seen: SeenRecord
		 
	  },
   ]
}

Reference Learning: https://www.tutorialspoint.com/mongodb/mongodb_data_modeling.htm https://docs.mongodb.com/manual/applications/data-models/

Clone this wiki locally