-
-
Save zenithtekla/819ddc7766761e2908b8487aa9c3cd9f to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema; | |
/** | |
* Schema | |
*/ | |
var Schema1 = new Schema({ | |
asset_number: String, | |
last_cal: Date, | |
schedule: Number, | |
next_cal: Date, | |
files: [{ | |
data: Buffer, | |
contentType: String | |
}], | |
model: String, | |
location: { | |
id: Number, | |
desc: String | |
} | |
}); | |
mongoose.model('Equipment', Schema1); | |
/* | |
I don't know the data and couldn't remember after days and couldn't derive the right relationship for the tables. | |
Days of burying our heads into the sand writing JOIN are passed as the logic later requires testing, fixing and re-validation. | |
I need to derive the right relationship to tie my Sequelize data models. | |
So, I went ahead and combined the 3 tables into the mongo Schema in 2 assumptive cases below. Software development deals a lot with assumptions | |
when I don't usually see real data fly into the tables. I need nothing else than a concatenated schema object. | |
*/ | |
DELIMITER // | |
CREATE PROCEDURE new_student_with_class_and_lession( | |
firstName CHAR(25), lastName CHAR(25), | |
className1 CHAR(25), period1 CHAR(25), | |
className2 CHAR(25), period2 CHAR(25), | |
lessionName1 CHAR(25), lessionName2 CHAR(25)) | |
BEGIN | |
START TRANSACTION; | |
INSERT INTO student_table (firstname, lastname) | |
VALUES(firstName, lastName); | |
SET lastStudentId = (SELECT LAST_INSERT_ID()); | |
INSERT INTO class_table (className1, period1, studentId) | |
VALUES(className1, period, lastStudentId); | |
SET lastStudentId = (SELECT LAST_INSERT_ID()); | |
INSERT INTO class_table (className2, period2, studentId) | |
VALUES(className2, period, lastStudentId); | |
INSERT INTO lession_table (lessionName1, classId) | |
VALUES(lessionName1, LAST_INSERT_ID()); | |
INSERT INTO lession_table (lessionName2, classId) | |
VALUES(lessionName2, LAST_INSERT_ID()); | |
COMMIT; | |
END// | |
DELIMITER ; | |
SELECT | |
lt.lessionName as LESSION | |
FROM lession_table lt | |
LEFT JOIN class_table ct | |
ON ct.classId = lt.classId | |
LEFT JOIN student_table st | |
ON st.studentId = ct.studentId | |
WHERE st.studentId = 1 | |
ORDER BY lt.id; | |
SELECT lt.lessionName as LESSION | |
FROM student_table as st | |
LEFT JOIN class_table as ct | |
ON st.studentId = ct.studentId | |
LEFT JOIN lession_table as lt | |
ON lt.classId = ct.classId | |
WHERE st.studentId = 1; |
zetekla
commented
Jan 11, 2018
•
Catalogs > Subjects > Bookmarks
id: {
catalogId: string,
userId: string
}
CatalogDocument {
_id: string,
userId: string,
editors: Array,
viewers: Array,
title: string,
name: string,
description: string,
createdAt: Date,
updatedAt: Date,
keywords: string, // some keywords
tags: string, // some tags
subjects: [ISubject]
}
id: {
subjectId: string,
userId: string
}
SubjectDocument {
_id: string,
userId: string,
editors: Array,
viewers: Array,
title: string,
name: string,
description: string,
createdAt: Date,
updatedAt: Date,
keywords: string, // some keywords
tags: string, // some tags
bookmarks: [IBookmark]
}
id: {
bookmarkId: string,
userId: string
}
BookmarkDocument {
_id: string,
userId: string,
editors: Array,
viewers: Array,
title: string,
name: string,
origin: string,
url: string,
description: string,
author: string,
status: string, // live-very-fast | live-fast | live-slow | live-very-slow | dead
note: string,
createdAt: Date,
updatedAt: Date,
keywords: string, // some keywords
tags: string, // some tags
}
editors is a collection of editorDocument {
_id: string, // userId, as the user types, typeahead suggests all the user, their name and email
name: string,
email: string // email is necessary for user to ensure the right person, because name does not ensure uniqueness,
badgeId: string // same purpose as to give visual cue and maintain uniqueness.
}
visitors is a collection of editorDocument {
_id: string, // userId, as the user types, typeahead suggests all the user, their name and email
name: string,
email: string // email is necessary for user to ensure the right person, because name does not ensure uniqueness,
badgeId: string // same purpose as to give visual cue and maintain uniqueness.
}
user should be able to view all records of Catalogs, Subjects and Bookmarks
CatalogUI should display all catalogs; (later) and a sneak peak of subjects
SubjectUI should display all subjects; (later) and a sneak peak of bookmarks
BookmarkUI should display all subjects; (later) and a sneak peak of bookmarks
user should be able to add/update/delete a record of BookmarkDocument
NoSQL Database: The Definitive Guide to NoSQL Databases
I would encourage you to be open-minded in using the variable in your objectID, as an object instead of just string. When I discovered this cool “trick” when playing with JSON when I tree my data as pieces of html codes but grouped by ObjectId, as variable. I.e. instead of using value: ‘foo’ in Petr’s answer, use this value: { foo }.
src:
https://www.quora.com/What-is-the-most-efficient-way-to-store-a-tree-data-structure-in-MongoDB
https://www.sisense.com/blog/postgres-vs-mongodb-for-storing-json-data/