Reputation: 7080
I have created two collections vendor and employee and vendor collection has references of employee "emplyoee_id:[String]" Should I do also vice versa? is that good idea? adding reference in both collections?
what If I want all employees with their company name along with it? I am confused.
employee
const employeeSchema = Schema({
_id: Schema.Types.ObjectId,
name: String,
address: String,
email: String,
designation: String,
contact_number: Number,
contract_start: Date,
contract_end: Date
});
Vendor
const vendorSchema = Schema({
_id: Schema.Types.ObjectId,
name: String,
address: String,
email: [String],
contact_number: [Number],
contract_start: Date,
contract_end: Date,
emplyoee_id:[String]
});
Upvotes: 1
Views: 168
Reputation: 1134
For loopback Framework
I have done it with Loopback framework. For example User collection and Project collection has taken For each user, there can be multiple projects and each project has one and only one user.
In project model
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "id"
}}
In user Model
"relations": {
"projects": {
"type": "hasMany",
"model": "project",
"foreignKey": "project_user_id"
}}
Upvotes: 0
Reputation: 189
In a mongoose model you can define properties with a reference to another model. Check mongoose popolate
Example:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const personSchema = Schema({
_id: Schema.Types.ObjectId,
name: String,
age: Number,
stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
const storySchema = Schema({
author: { type: Schema.Types.ObjectId, ref: 'Person' },
title: String,
fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});
const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);
Upvotes: 2