Reputation: 1
I have decided to help my friend with a website to his new company, and I figured it's a good time to try out the MERN-stack (mongodb, express, react and node.js).
But then again I'm thinking about MongoDB and if it's really optimal for this project, because there will be relational data.
The relational data there will be is customers and their objects. Let's say customers that register, and to every customer there will be one or several cars that the company uses in some way.
Is this doable in MongoDB? How would you go about structuring it in the best way? I'm thinking about a collection of users, having the objects (cars) as a field of object arrays in the user collection.
I haven't done a project in MERN before, and I'm open to any suggestion.
Also I would be very interested in having a 'mentor', I could even be open to some kind of payment having someone mentor me through this project. I hope this is not against the rules here, if it is, sorry.
Thank you.
Upvotes: 0
Views: 196
Reputation: 2166
In order to handle relational data in MongoDB, you will have to take advantage of the DBrefs
feature.
I am taking thos example straight from one of my projects in production.
The schema is heavily truncated but still tells the story of the DBrefs
feature.
It consists of two schemas: activity & user
The activity schema is here:
/*eslint-env node*/
//Dependencies
const mongoose = require('mongoose');
//Global Constant
const Schema = mongoose.Schema;
// Create Schema
const ActivitySchema = new Schema({
activity: {
type: String,
},
description: {
type: String,
},
intensity: {
type: String,
},
hostId: {
type: Schema.Types.ObjectId,
ref: 'user'
},
}, {
collection: 'Activity'
});
mongoose.model('activity', ActivitySchema);
The user schema is here:
/*eslint-env node*/
//Dependencies
const mongoose = require('mongoose');
//Global Constant
const Schema = mongoose.Schema;
//Create Schema
const UserSchema = new Schema({
name: {
type: String,
},
nickName: {
type: String
},
email: {
type: String,
},
password: {
type: String,
},
profImage: {
type: String,
},
interests: {
type: Array,
},
}, {
collection: 'User'
});
mongoose.model('user', UserSchema);
As you can see, there is a field called hostId
at the end of the activity schema with a type of Schema.Types.ObjectId
and a ref:
field which points to the user schema.
What it does essentially is to virtually link the two documents together by storing the ObjectId
of the user in the activity collection (called a table in SQL).
The two documents are only actually linked together when and only when a query function, for example,.findOne()
is called along with the .populate()
aggregation operator.
Example:
//Syntax: database.collection.queryFunction
//Normal Query:
db.activity.findOne({activity:'stack overflow event`})
.then(activityData=>{
console.log(activityData);
//You will get something like this:
[
{
activity: 'stack overflow event',
description: 'It's all about SO',
intensity: 'Very chill',
hostId: ObjectId(5ea1e3c89dcab6c27d1eb891),
}
]
})
.catch(err=>console.log(err))
//When you use .populate()
db.activity.findOne({activity:'stack overflow event`})
.populate('user')
.then(activityData=>{
console.log(activityData);
//You will get something like this:
[
{
activity: 'stack overflow event',
description: 'It's all about SO',
intensity: 'Very chill',
//The hostId field will get populate with the user object
hostId: {
name: 'SO Test User',
nickName: 'soTest',
email: '[email protected]',
....it continues
}
}
]
})
.catch(err=>console.log(err))
Upvotes: 0