There are two collections Employee and 'Project', It need to maintain history of project allocations for employee as well as projects basis.
I have created collection schema but it duplicates the data, is there any other way I can achieve this?
Employee Schema
const employeeSchema = mongoose.Schema({
name: {
type: String,
required: true
},
project: {
start: {
type: Date,
required: true
},
end: {
type: Date,
required: true
},
project: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Project',
required: true
}
},
}
Project Schema
const projectSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
employee: {
start: {
type: Date,
required: true
},
end: {
type: Date,
required: true
},
project: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Employee'
}
}
});
Or Can I create separate collection for mapping both project and employee? if I do this then what is the difference between MongoDB and RDBMS?
Related
Here is my very basic product schema:
const productSchema = new Schema({
productName: {
type: String,
required: true,
},
productDescription: {
type: String,
},
productPrice: {
type: Number,
},
});
module.exports = mongoose.model("Product", productSchema);
These products are listed out and a user can add a quantity for each. I am storing in an array of objects as per below. I want to join these two collections together so that I can output the qty of products selected by the user. I believe I need to use populate here but not sure how to set up the Refs and so on.
const PartySchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
catering: [{ id: mongoose.Types.ObjectId, qty: Number }],
created_at: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Party", PartySchema);
I'm sharing this solution with the assumption that the catering field is the sub-document array pointing to the Product Schema:
The Product Schema is fine so it stays the same (although to keep to convention I would advice naming your schema 'Products' instead of 'Product', Mongo Naming Covention):
const productSchema = new Schema({
productName: {
type: String,
required: true,
},
productDescription: {
type: String,
},
productPrice: {
type: Number,
},
});
module.exports = mongoose.model("Products", productSchema);
And next the Party Schema would be:
const PartySchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
catering: [{
id: {
type: mongoose.Types.ObjectId,
ref: 'Products',
},
qty: {
type: Number,
}
}],
created_at: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Parties", PartySchema);
I have 2 models - User & Project.
Relations are -
Users can have multiple projects (One to many)
One projects may be shared with multiple other users. (Many to many)
const UserSchema = new Schema({
name: {
type: String,
required: true,
},
mobile: {
type: Number,
required: true,
unique: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true,
},
role: {
type: String,
default: 'basic',
enum: ["basic", "admin"]
},
projects: [
{
type: Schema.Types.ObjectId,
ref: 'Project'
}
],
shared_projects: [
{
type: Schema.Types.ObjectId,
ref: 'Project'
}
]
}, { timestamps: true });
const ProjectSchema = new Schema({
name: {
type: String,
required: true,
},
active: {
type: Boolean,
default: false,
},
expiry: {
type: Date,
default: null,
},
owner: {
type: Schema.Types.ObjectId,
ref: 'User'
},
shared_users: [
{
type: Schema.Types.ObjectId,
ref: 'User'
}
]
}, { timestamps: true });
I want to have a feature that the owner of the project may disable one or many shared user. So, I need a status field in ProjectSchema like -
shared_users: [
{
type: Schema.Types.ObjectId,
ref: 'User'
},
status: {
type: Boolean,
default: true
}
]
So, do I need to have the same definition in UserSchema also & update both schemas when user disable or enable a shared user?
I just started learning express and mongodb. I recently faced the kind of problem, I'm trying to select all the subdocuments which are inside of Room model.
const books = await Room.find().populate('book');
But it returns whole room document when i want to select only bookings field.
Here's the book schema
const bookSchema = new mongoose.Schema({
startDate: {
type: Date,
required: true,
},
endDate: {
type: Date,
required: true,
},
name: {
type: String,
required: true,
},
phone: {
type: String,
required: true,
},
});
module.exports = mongoose.model("book", bookSchema)
And here's the room schema
const roomSchema = new mongoose.Schema({
currentlyReserved: {
type: Boolean,
default: false,
},
people: {
type: Number,
required: true,
},
roomNumber: {
type: Number,
required: true,
},
pricePerPerson: {
type: Number,
required: true,
},
reservedUntil: {
type: Date,
default: null,
},
reservedBy: {
type: String,
default: null,
},
bookings: {
type: [{ type: mongoose.Schema.Types.ObjectId, ref: "book" }],
},
});
module.exports = mongoose.model("room", roomSchema);
You can project with with second arg to find().
https://docs.mongodb.com/manual/reference/method/db.collection.find/#projection
const books = await Room.find({}, {bookings: 1}).populate('bookings');
Below is my mongoose schema.
An application can have multiple incidents and subscribers. Each incident can have multiple journal updates.
Application->incident(s)->journalupdate(s)
My goal is to allow users to update an incident detail in an application. Later add a journal update to an incident in the application.
const JournalUpdateSchema = mongoose.Schema({
timeStamp: { type: Date, required: true },
owner: { type: String },
updateText: { type: String }
});
// Incident Schema
const IncidentSchema = mongoose.Schema({
incidentNum: { type: String, required: true },
incidentPriority: { type: String, required: true },
description: { type: String, required: true },
jounralUpdates: [JournalUpdateSchema]
});
//Subscriber Schema
const SubscriberSchema = mongoose.Schema({
subscriberName: { type: String },
subscriberMail: { type: String },
subscriberPhone: { type: String }
});
// Application Schema
const ApplicationSchema = mongoose.Schema({
applicationName: { type: String, required: true },
clusterName: { type: String, required: true },
ragStatus: { type: String, required: true },
incidents: [IncidentSchema],
subscribers: [SubscriberSchema]
});
I have already tried Applications.Update(), Applications.findByIdAndUpdate() methods but I cannot reach the incident level to update.
can someone help me with this please? Any help is greatly appreciated.
Thanks.
I'm using mean stack to create a hybrid app.I'm using nosql to create DB in mongoose.My DB consists of two tables one is 'donors' and another one is 'bloodGroup'.
My 'bloodGroup' schema is as follows:
module.exports = function(mongoose) {
var Schema = mongoose.Schema;
/* bloodGroup Schema */
var bloodGroupSchema = new Schema({
name: { type: String, required: true }
});
}
My 'Donor'schema is as follows:
/* Donor Schema */
var DonorSchema = new Schema({
Name: { type: String, required: true },
DOB: { type: Date, required: true, trim: true },
Sex: { type: String },
BloodGroupID: { type: Schema.Types.ObjectId, ref: 'BloodGroup', required: true },
ContactNo: { type: String, required: true },
LocationId: { type: Schema.Types.ObjectId, ref: 'Location', required:true },
EmailId: { type: String, required: true },
Password: { type: String, required: true }
});
When many donors refer to a single blood group then BloodGroup object Id error is reported.How to solve this problem?
You can refer this link for documentation: http://mongoosejs.com/docs/populate.html
Saving Refs
/* Donor Schema */
var DonorSchema = new Schema({
_id : {type: Number},
Name: { type: String, required: true },
DOB: { type: Date, required: true, trim: true },
Sex: { type: String },
BloodGroupID: { type: Schema.Types.ObjectId, ref: 'BloodGroup', required: true },
ContactNo: { type: String, required: true },
LocationId: { type: Schema.Types.ObjectId, ref: 'Location', required:true },
EmailId: { type: String, required: true },
Password: { type: String, required: true }
});
/* bloodGroup Schema */
var bloodGroupSchema = new Schema({
_bid :{ type: Number, ref: 'Donor' },
name: { type: String, required: true }
});
module.exports = mongoose.model('Donor', DonorSchema);
module.exports = mongoose.model('Blood', bloodGroupSchema);
var vidya = new Donor({ _id: 0, name: 'Vidya', sex: 'F' });
vidya.save(function (err) {
if (err) return handleError(err);
var blood = new BloodGroup({
name: 'B+',
_bid: vidya._id // assign the _id from the Donor
});
blood.save(function (err) {
if (err) return handleError(err);
// thats it!
});
});
Mongo is not a Relational database, relation one to many does not exist in mongDB. The question is quite confusing, but following the title, you should either embed the donnors into the BloodGroup, or create an Id field unique to which you will refer and do two queries.