Students schema has an array of objects (tests) administered. No two tests are alike, and a student can have multiple tests. Need to be able to insert/update array of [test] objects with different schema's.
Have tried placing each test into it's own Collection tying back to the Student Id, but there are hundreds of unique tests resulting in hundreds of collections. Best practice?
const StudentSchema = new SimpleSchema({
lname: {
type: String,
optional: false,
label: "Last Name",
max: 100
},
fname: {
type: String,
optional: false,
label: "First Name",
max: 100
},
tests: {
type: Object,
blackbox: true,
optional: true,
},
}, { tracker: Tracker });
//Tests Schema example 1------------------------
TestADT_Schema = new SimpleSchema({
total_ADT_score_rs: {type: Number, optional: true,},
total_ADT_score_tsc: {type: Number, optional: true,},
qualitative_score: {type: Number, optional: true,},
});
Test_ADT_Schema.attachSchema(ADT_Schema);
//Test Schema example 2 --------------------------
TestWJIVCA_Schema = new SimpleSchema({
GenIntellectualAbility_AE: {type: Number, optional: true,},
GenIntellectualAbility_GE: {type: Number, optional: true,},
GenIntellectualAbility_RPI: {type: Number, optional: true,},
GenIntellectualAbility_SS: {type: Number, optional: true,},
GFGC_COMPOSITE_AE: {type: Number, optional: true,},
GFGC_COMPOSITE_GE: {type: Number, optional: true,},
GFGC_COMPOSITE_RPI: {type: Number, optional: true,},
});
TestWJIVCA_Schema.attachSchema(TestsWJIVCASchema);
Each student having 1 to n Tests, needing to nest the Tests into the Student's Collection, but not sure if this can be done?
If tests is an array that contains mysterious objects, you should mark the tests as an array and it's children as the blackbox object:
const StudentSchema = new SimpleSchema({
lname: {
type: String,
optional: false,
label: "Last Name",
max: 100
},
fname: {
type: String,
optional: false,
label: "First Name",
max: 100
},
tests: Array,
'tests.$': {
type: Object,
blackbox: true,
},
}, { tracker: Tracker });
Related
Please tell me what I'm missing here. When I update the Array of objects my collection updates with an empty array. I'm using meteor-collection2-core and node-simple-schema.
Path: Method
testArray = [{
"institution": "Swinburn",
"uniqueId": 1501036480813,
"qualification": "Bachelor of Commerce",
"completed": "2017"
}]
ProfileCandidate.update('Ntzj6kE8qZsvSPMEP', {$set: {educationQualifications: testArray}});
Path: Collection
export const ProfileCandidate = new Mongo.Collection('profileCandidate');
const ProfileCandidateSchema = new SimpleSchema({
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id
},
educationQualifications: { type: Array, optional: true },
'educationQualifications.$': { type: Object, optional: true },
'educationQualifications.$.uniqueId': { type: Number, optional: true },
'educationQualifications.$.institution': { type: String, optional: true },
'educationQualifications.$.qualification': { type: String, optional: true },
'educationQualifications.$.completed': { type: String, optional: true }
});
ProfileCandidate.attachSchema(ProfileCandidateSchema);
In your method, try this:
ProfileCandidate.update({userId: 'Ntzj6kE8qZsvSPMEP'}, {$set: {educationQualifications: testArray}});
I have a Meteor method which does an API call, then the response of the call is saved to the users collection. I'm using the Collection2 package with my project and I'm a little bit lost setting up my SimpleSchema for this.
Here is what the JSON response looks like from the API call:
[{"keyword":"2i","url":"http://example.com","title":"Example","timestamp":"2016-11-05
08:54:42","ip":"00.00.00.000","clicks":"2","user":"HweoSCY2ujscjJ9Zl"},{"keyword":"2j","url":"http://example.com","title":"YouTube","timestamp":"2016-11-06
02:11:18","ip":"00.00.00.000","clicks":"1","user":"HweoSCY2ujscjJ9Zl"},{"keyword":"2k","url":"http://example.com","title":"YouTube","timestamp":"2016-11-08
03:35:12","ip":"00.00.00.000","clicks":"0","user":"HweoSCY2ujscjJ9Zl"}]
Here's currently how I've been able to save this data to the users collection:
Meteor.users.update(Meteor.userId(), {
$set: { 'shortURLs.URLS': result.data }
});
This works and looks like this in the db:
My issue is that I'd like to have a SimpleSchema setup for this so that the "timestamp" will be saved as a Date instead of a String, but everytime I try and create a schema for it I just receive errors like "After filtering out keys not in the schema, your modifier is now empty". I have tried a lot of different variations to try and make it work but none of them have been successful, here's just currently where it's at:
Schema.ShortURLs = new SimpleSchema({
shortURLs: {
type: Object
},
'shortURLs.$': {
type: Object
},
'shortURLs.$.keyword': {
type: String,
optional: true,
label: "Keyword"
},
'shortURLs.$.url': {
type: String,
optional: true,
label: "URL"
},
'shortURLs.$.title': {
type: String,
optional: true,
label: "Title"
},
'shortURLs.$.timestamp': {
type: Date,
optional: true,
label: "Timestamp"
},
'shortURLs.$.ip': {
type: String,
optional: true,
label: "IP"
},
'shortURLs.$.clicks': {
type: String,
optional: true,
label: "Clicks"
},
'shortURLs.$.user': {
type: String,
optional: true,
label: "User"
},
});
This is then attached apart of a User Simple Schema:
...
shortURLs: {
type: Schema.ShortURLs,
optional: true
},
...
And I have that attached to the users collection:
Meteor.users.attachSchema(Schema.User);
I don't think there's an issue with how I have it attached as I have other SimpleSchemas setup the same way and they're working fine, I believe the issue is how I have this particular one written. Any help here would be extremely appreciated.
You need to define the shortURLs type in the Meteor.users collection as type: [Schema.ShortURLs], i.e. a list of type Schema.ShortURLs
Schema = {}
Schema.ShortURLs = new SimpleSchema({
keyword: {
type: String,
optional: true,
label: "Keyword"
},
url: {
type: String,
optional: true,
label: "URL"
},
title: {
type: String,
optional: true,
label: "Title"
},
timestamp: {
type: Date,
optional: true,
label: "Timestamp"
},
ip: {
type: String,
optional: true,
label: "IP"
},
clicks: {
type: String,
optional: true,
label: "Clicks"
},
user: {
type: String,
optional: true,
label: "User"
},
});
Schema.User = new SimpleSchema({
...
shortURLs: {
type: [Schema.ShortURLs],
optional: true
},
...
});
...
Meteor.users.attachSchema(Schema.User);
my SimpleSchema.
Products.schema = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id
},
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
denyUpdate: false
},
tags: {
type: Object,
optional: true,
blackbox: true,
label: 'Categoria del producto'
}
});
Product array insert.
Let tags = ["7524", "5249", "7324"];
returns Error :tags product must be an object
with this type of object if it works:
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
but I want to insert an object like this:
let tags = ["7524", "5249", "7324"];
I HOPE YOUR ANSWER AND GREETINGS.
You need to specify the field with the object in this way. Then you can input the array.
Products.schema = new SimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id
},
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
denyUpdate: false
},
tags: {
type: Array,
optional: true,
label: 'Categoria del producto'
},
"tags.$": {
type: Object,
}
});
I might be thinking about this the wrong way, so please feel free to correct my thinking.
I'm using simpleSchema and I have a section of code which is used in more than one schema. Is there a way to create an individual component and import it into each schema, so that when I need to update the component I don't have to update it in multiple locations?
Path: resuableComponent
type: String,
optional: true,
autoform: {
type: "select",
options: function () {
return [
{label: "School logo 1", value: 'url'},
{label: "School logo 2", value: 'url'},
{label: "School logo 3", value: 'url'},
];
},
}
Path: studentCollection.js
Schemas.Student = new SimpleSchema({
studentUserId: {
type: String,
},
school: {
type: String,
optional: false
},
**resuableComponent**
});
Path: teacherCollection.js
Schemas.Teacher = new SimpleSchema({
teacherUserId: {
type: String,
},
school: {
type: String,
optional: false
},
**resuableComponent**
});
You could move the reusable objects into a different file that should be visible on both client and server if you are using SimpleSchema.
Example based on your question:
lib/schema-components.js :
SchemaComponents = {
school: {
type: String,
optional: false
},
// ...
// more reusable components here
};
someCollectionFile.js :
Schemas.Student = new SimpleSchema({
studentUserId: {
type: String
},
school: SchemaComponents.school,
// ...
});
I have a schema with a field type: Object . But whenever i do an insert, that object is empty.
Here is my schema
Contacts.attachSchema(new SimpleSchema({
firstName: {
type: String,
},
lastName: {
type: String,
optional: true
},
twitterFriend: { // this field
type: Object,
optional: true
}
}));
Even if do Contacts.insert({firstName: 'Mustafa', twitterFriend: {test: 'this should be stored'}}) . It does not work.
For an object of arbitrary sub-schema you set blackbox: true
Contacts.attachSchema(new SimpleSchema({
firstName: {
type: String,
},
lastName: {
type: String,
optional: true
},
twitterFriend: { // this field
type: Object,
optional: true,
blackbox: true
}
}));
See SimpleSchema docs for reference.