Reputation: 51
I want to make a small full-stack home project. If the project turns out well I want to add it to my portfolio when I apply for jobs. My plan is to use the MERN-stack to build a web-app where users can create their own logins, store what flowers they have and how often they need to water such flowers. Creating a watering schedule would be the next step.
I have just started using mongoDB. I am wondering how to best structure the data since different users should have access to their own flowers.
Would it be possible to have both the flowers and the associated user in the same MongoDB-document?
Is it possible to link documents similar to SQL databases?
The following is my approach using the first alternative:
{
name: "Joe Flowerman",
flowers: [
{
name: "Freesia",
water_freq_month: 4
},
{
name: "Boat orchid",
water_freq_month: 8
}
]
}
I am not sure if this is the optimal usage of such data, so I welcome any opinions.
Upvotes: 1
Views: 255
Reputation: 1041
if you want to handle this types of structure so you must Use 2 Collection
like 1 for user & 2 for Flowers(inventories)
{
"_id":ObjectId("5ddbcaaf7b9b9c4b986f9e89"),
"name": "Joe Flowerman",
"flowers":
[ObjectId('5ddbcaaf7b9b9c4b986f9e42'),ObjectId('5ddbcaaf7b9b9c4b986f9e43')]
}
- FlowerCollection
{
"_id": ObjectId('5ddbcaaf7b9b9c4b986f9e42'),
"name": "Freesia",
"water_freq_month": 4
},
{
"_id": ObjectId('5ddbcaaf7b9b9c4b986f9e43'),
"name": "Boat orchid",
"water_freq_month": 8
}
If you can use this type of structure then data redundancy can be removed
when you want to retrieve the data then use simple aggregate query
Upvotes: 1
Reputation: 17868
Both options are possible.
If you need to display users and theirs flowers, embedding flowers inside user would be fine.
But if you need to display all flowers, it is better to keep flowers in a seperate document.
Let's say you don't need to display all flowers, and choose the embedding.
You can create the following mongoose schema for user:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
flowers: [
new mongoose.Schema({
name: {
type: String,
required: true
},
water_freq_month: {
type: Number,
required: true
}
})
]
});
module.exports = mongoose.model("User", userSchema);
You can add flower to the user, and get user info with flowers with the following code:
const router = require("express").Router();
const User = require("../models/user");
router.post("/users/flowers", async (req, res) => {
const loggedUserId = "5ddbcaaf7b9b9c4b986f9e89";
const { name, water_freq_month } = req.body;
const result = await User.findByIdAndUpdate(
loggedUserId,
{
$push: {
flowers: { name, water_freq_month }
}
},
{ new: true }
);
res.send(result);
});
router.get("/users/flowers", async (req, res) => {
const loggedUserId = "5ddbcaaf7b9b9c4b986f9e89";
const user = await User.findById(loggedUserId);
res.send(user);
});
module.exports = router;
When we add two flowers to the user the response will be like this when we get the user:
{
"_id": "5ddbcaaf7b9b9c4b986f9e89",
"name": "Joe Flowerman",
"flowers": [
{
"_id": "5ddbcacc1640bf4b10f985cb",
"name": "Freesia",
"water_freq_month": 4
},
{
"_id": "5ddbcafba209c91b5cbf3b4c",
"name": "Boat orchid",
"water_freq_month": 8
}
],
"__v": 0
}
Upvotes: 1