Skip to content

Instantly share code, notes, and snippets.

@thehme
Last active July 20, 2018 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thehme/b34b9657dedafaaf4f963b6d5ec4c805 to your computer and use it in GitHub Desktop.
Save thehme/b34b9657dedafaaf4f963b6d5ec4c805 to your computer and use it in GitHub Desktop.
// schema
var DataTypes = new Schema({
name: {type: String, required: true},
types: [{
type: {type: String, required: true},
units: {type: String},
devices: [{type: String}],
threshold: {type: Number},
numberPerDay: {type: Number, default: 3}
}]
}, {_id: false, versionKey: false});
var Program = new Schema({
pid: {type: Number, required: true, index: true},
dataTypes: {type: [DataTypes], required: true, index: true},
users: [ProgramUser]
}, {timestamps: true});
// program example
{
"pid" : 220,
“users” : [
{
"userId" : 693,
"dateSignedUp" : ISODate("2017-10-19T21:55:31.851Z")
},
{
"userId" : 694,
"dateSignedUp" : ISODate("2017-10-20T13:37:26.694Z")
}
],
"dataTypes" : [
{
"name" : "activity1",
"types" : [
{
"type" : "steps",
"units" : "steps",
"threshold" : 0.9,
"numberPerDay" : 1,
"devices" : [
"device1"
]
}
]
},
{
"name" : “activity2”,
"types" : [
{
"type" : "timeAsleep",
"units" : "hh:mm",
"threshold" : 0.9,
"numberPerDay" : 1,
"devices" : [
"device1"
]
}
]
}
]
}
// code
let main = async function() {
let programs = [];
try {
programs = await data.aps.Programs.find({"dataTypes.types.threshold":{"$lte":1}}).exec();
console.log("There are " + programs.length + " programs in APS with thresholds <= 1");
} catch (e) {
console.log("Error retrieving programs with thresholds <= 1");
console.log(e);
}
try {
for (let program of programs) {
let cond = { _id: program._id };
let update = { $set: { } };
program.dataTypes.forEach(async function(dataType, index1) {
dataType.types.forEach(async function(type, index2) {
console.log("Before: " + program.dataTypes[index1].types[index2].threshold);
update.$set[`program.dataTypes.${index1}.types.${index2}.threshold`] = type.threshold * 100;
});
});
await program.update(cond, update);
}
console.log("Updated threshold value for all programs.")
} catch (e) {
console.log("Error correcting the program thresholds.");
console.log(e);
}
};
main()
.then(() => process.exit())
.catch((e) => console.log(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment