I have been doing back end for a hospital prescription management and I used mongo and node for that.. when using the mongo schema, not knowing how many drugs will the doctor write I have to manually write all the drugs for the schema.. so is there a way to automate it .. am new to mongo so help me please.
const presmod = mongoose.Schema({
HospName : {
type: String,
required : true
},
DoctorId: {
type : String,
required : true
},
PatientName : {
type: String,
required : true
},
PatientId : {
type: String,
required : true
},
drugs : {
drug1 : {date:{type: Date, required: true},Name: {type: String, required: true}},
drug2 : {date:{type: Date, required: true},Name: {type: String, required: true}},
drug3 : {date:{type: Date, required: true},Name: {type: String, required: true}},
drug4 : {date:{type: Date, required: true},Name: {type: String, required: true}},
drug5 : {date:{type: Date, required: true},Name: {type: String, required: true}},
drug6 : {date:{type: Date, required: true},Name: {type: String, required: true}},
},
})
module.exports = mongoose.model("prescription", presmod)
instead of manually typing drug 1, drug 2 ... drug n ,.. is there a way to automate it?
using array you can define it like this:
drugs: [{
date: {
type: Date,
required: true
},
Name: {
type: String,
required: true
}
}]
Related
I'm trying to make a form-maker with Node.Js and MongoDB but confused about the data structure.
By the way, I created a form.model to store the form structure:
{
title: {
type: String,
required: [true, 'Form must have a title']
},
users: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
role: Number
}],
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
active: {type: Boolean, default: true},
fields: [{
case_id: String,
label: String,
type: Number, //1:selective, 2:descriptive, 3:range
required: Boolean,
default: {
title: String,
value: Number,
},
placeholder: String,
items:[{
label: String,
value: Number,
}],
range:[{
min: Number,
max: Number,
step: Number,
default: {type: Number, default: 0}
}]
}]
}
and a form.data.model to store the data of forms:
{
form: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Form',
required: true
},
fields_data: {
"case_id": Object //value per case_id
}
}
Is that true? or there is the best practice for that?
If I modify the form fields how control the data about?
Finally, I've created the structure of my form-maker data.
form.model:
{
title: {
type: String,
required: [true, 'Form must have a title']
},
owner: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: [true, 'Form must have a owner']
},
active: {type: Boolean, default: true},
deleted: {type: Boolean, default: false},
fields: [{
name: String, //that field id and must be auto generated and not be modified by end user
label: String,
type: Number, //1:Selective, 2:Descriptive, 3:Range
required: Boolean,
placeholder: String,
selectable_items:[{
label: String,
value: Number,
selected: Boolean
}],
range:[{
min: Number,
max: Number,
step: Number,
default: {type: Number, default: 0}
}]
}]
}
form.users.model:
{
form: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Form',
required: true
},
users: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
role: Number //0:Admin, 1:Editor, 3:Viewer
}
]
}
form.values.model:
{
form: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Form',
required: true
},
values: [
{
name: String,
value: Object
}
]
}
Hello, I hope this question ain't stupid, but I have a problem with Nodejs/mongoose/MongoDB.
var SportsSchema = new Schema({
title: String,
internalTitle : String,
semester : String ,
uniqueIdent : { type : String, unique : true, index : true },
sportsType: String,
kindOfSport: String,
description: String,
location: String,
numberOfParts: Number,
recurrence: String,
weekDay1: {type: String, default: 'empty'},
weekDay2: {type: String, default: 'empty'},
startTime1: {type: String, default: 'empty'},
startTime2: {type: String, default: 'empty'},
sportDates: [{
type: Date
}],
specDates: [{
type: String
}],
timeFrame: [{
type: Date
}],
created: {type: Date, default: Date.now },
});
This is the Schema. Only uniqueIdent is unique and indexing. As soon as the nodeJS app starts, the mongoDB collection has the following indexes: _id, uniqueIdent and username. While the first two are expected, the third one is flabbergasting me.
There is a username in the other schema that i am using, but that shouldn't influence this one, should it.
Any help would be appreciated, if more information is necessary, I will happily provide it.
other schema:
var UserSchema = new Schema({
username: {type: String, lowercase: true, unique: true, match: [/\S+#\S+\.\S+/, 'ist nicht erlaubt']},
email: {type: String, lowercase: true, unique: true, match: [/\S+#\S+\.\S+/, 'ist nicht erlaubt']},
firstname :String,
lastname: String,
password: String,
//admin: {type: Boolean, default: false },
permissionLevel: {type: String, default: 'user' },
tr_tmp: {type: Boolean, default: 'false'},
classOne: {type: String, default : 'empty' },
classTwo: {type: String, default : 'empty' },
classThree: {type: String, default : 'empty' }
});
I am trying to do schema validation in mongoose of type object and array, but I am not able to do that. Here is the schema:
var alertEmailSchema = new alertEmailSchema({
templateId: { type: String, required: true,unique: true},
templateName : { type: String, required: true},
status: Boolean,
frequency : { type: Object, required: true},
recipientsEmailId : { type: [String], default: [], required: true},
subject : { type: String, required: true},
message : { type: String, required: true},
description : String,
createdDate : {type : Date, default : Date.now},
updatedDate : {type : Date, default : Date.now}
});
var schemaValidation = newAlertEmail.validateSync();
Please tell me how can I do the validation for this.
I assume you want something like this:
var frequency = new Schema({
count: {
type: Number
},
updateAt: {
type: Date
}
}, {
_id: false // this will not create _id for this schema
});
var alertEmailSchema = new Schema({
templateId: { type: String, required: true,unique: true},
templateName : { type: String, required: true},
status: Boolean,
frequency : frequency, //this can be an array also [frequency]
recipientsEmailId : { type: [String], default: [], required: true},
subject : { type: String, required: true},
message : { type: String, required: true},
description : String,
createdDate : {type : Date, default : Date.now},
updatedDate : {type : Date, default : Date.now}
});
Here, I created another schema known as frequency and used it in schema alertEmailSchema.
I have 2 models:
Here is the User Model:
const userSchema = new mongoose.Schema({
email: { type: String, unique: true, required: true },
password: { type: String, required: true },
passwordResetToken: String,
passwordResetExpires: Date,
facebook: String,
twitter: String,
tokens: Array,
profile: {
name: String,
gender: String,
location: String,
website: String,
picture: String
}
}, { timestamps: true });
And here is the Revive Model:
const reviveSchema = new mongoose.Schema({
reviveShowName: {type: String, required: true},
reviveTitle: {type: String, required: true},
reviveCategory: {type: String, required: true},
reviveGoal: {type: Number, required: true},
revivePhoto: {type: String, required: true},
reviveVideo: {type: String},
reviveStory: {type: String},
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
name: String
}
}, { timestamps: true });
In the Revive model, I'm trying to the reference the author and get the author's id and that works... How do I also get the name from profiles -> name...? Clearly name: String is wrong...
Mongoose relations work, based on the ref and type value of the nested object. In your case you have associated the id property of author to point to the User model.
If you want to populate the author with the user information, you should just do :
author: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
Then in your query you just use populate
Revive.find({})
.populate( 'author' )
.exec( function( error, docs ) {
console.log( docs ); // will have `[{author:{profile:{...}}}]` data
} );
I need to provide search for e-commerce site. In that there is a 4-level menu like this category>>subcategory>>product>>subproduct. I had divided them into 3 schemas like this
category schema
var categorySchema = new mongoose.Schema(
{
"catname": {type: String, required: true},
}
);
mongoose.model('category',categorySchema);
subcategory and product schema
var productSchema = new mongoose.Schema(
{
"catid": {type: mongoose.Schema.Types.ObjectId, ref: 'category'},
"subcatname": {type: String, required: true},
"product":[
{
"productname":{type:String},
}
]
}
);
mongoose.model('product',productSchema);
subproduct schema
var subproductSchema = new mongoose.Schema(
{
"productid": {type: mongoose.Schema.Types.ObjectId, ref: 'products', required: true},
"itemcode": {type: String, required: true},
"itemname": {type: String, required: true/*, index: "text"*/},
"itemdescription": {type: String, required: true},
"itemimgurl": {type: String, required: true},
"mainprice": {type: Number},
"offerprice": {type: Number, required: true},
"unit": {type: String, required: true},
"stock": {type: Number, required: true},
"offertag": {type: String},
"itemprice":[
{
"itemname" :{type: String, required: true},
"unit": {type: String, required: true},
"offerprice": {type: Number, required: true},
"mainprice": {type: Number},
"stock": {type: Number, required: true},
"notify": {type: Number, required: true},
"status": {type: Boolean, required: true},
"itemimgurl": {type: String},
"offertag": {type: String}
}
]
}
);
mongoose.model('subproduct',subproductSchema);
I need to give the subproducts corresponding to the users search, like if the user searches with the catname or subcatname or productname or itemname need to give the related subproducts. How can I achieve this with mongoose?
This is with reference to the question you asked in the comment.
If you want to search for an incomplete query or a word, i will provide a small code, which will help you.
var query = req.body.query; //the string you take for search
var filter = {
$or: [
{subcatname: new RegExp('.*' + query, 'i')},
{productname: new RegExp('.*' + query, 'i')},
//similarly you can put more cases
]
}
subproducts.find(filter, function(err, data){
if(err){console.log(err);}
console.log(data);
});