Creating a MongoDB blueprint that incorporates users, brands, and data ownership: a comprehensive guide

I am currently in the initial stages of developing a node.js/react application that will assist users in managing restaurant menu data. I am contemplating on how to properly organize user accounts, restaurant brand accounts, and data ownership.

Individual users should be able to log in and authenticate themselves.

The brand account should be controlled by users, allowing multiple users to access the brand account data.

Data ownership should belong to the brand account, ensuring that only authorized users associated with that brand can view the data.

What are some best practices for establishing these relationships within the schema?

My idea is to have models such as User, Account, and various other data models. For instance, let's take MenuItems:

Would this simplified example be the correct approach?:


        User {
            email: String,
            password: String
        }

        Account {
            account_name: String,
            users: [User1, User2 ...] // Embedded documents or references
        }

        MenuItems {
            title: String,
            description: String,
            account: [Account] // Embedded document or reference
        }
    

Answer №1

If you're looking to connect data in your database, consider using the populate method in Mongoose: https://mongoosejs.com/docs/populate.html

For 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);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Frontend React app encountering communication issue with backend API via proxy connection

Error: Request to /api/v1/products from localhost:3000 could not be proxied to . Refer to https://nodejs.org/api/errors.html#errors_common_system_errors for details (ETIMEDOUT). This issue persists. Frontend -> React Backend -> Express, Node.js ...

Struggling with implementing private npm modules using require() and import

Trying to import a private npm module hosted in sinopia, I can see it available in the sinopia folder structure. I successfully installed the module via "npm install --save", and it seems to pick it up from the local registry. However, when attempting to ...

Is it optimal to have nested promises for an asynchronous file read operation within a for loop in Node.js?

The following Node.js function requires: an object named shop containing a regular expression an array of filenames This function reads each csv file listed in the array, tests a cell in the first row with the provided regular expression, and returns a n ...

Proceed with Command Line Interface (CLI) operations once the file has

Is there a way to make Node.js keep receiving input like a typical CLI when we call it. $ nodejs For instance: Below is an index.js file // index.js var a = 10 goToCLI() I am expecting that when I execute $ nodejs ./index.js, a Node.js CLI will open wi ...

Implementing ExpressJS with MongoDB on a MERN Development Stack

After configuring my ExpressJS & MongoDB client and running Nodemon, I consistently encounter the following warning: "DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the ...

Error: Expected an Image or Canvas as input

The code snippet below shows the function I was using in Express with request params text to load an image, but unfortunately it's not working as expected. const { Canvas } = require('canvas-constructor') const canvas = require('canvas& ...

When running the command "babel server --out-dir build", Babel does not compile .graphql files located within the build directory

Introduction: Our application utilizes Heroku as the server platform. After pushing code, it triggers npm start according to the package.json file. My current nodeJS version on my laptop is v8.16.2, with npm at 6.4.1. In the package.json file: "engines ...

I encountered an issue while trying to start my Nuxt project - facing difficulty in resolving 'fsevents' within the project directory

Whenever I start my nuxt.js project using npm run dev, I encounter compilation errors: × Client Compiled with some errors in 12.53s WARN Compiled with 2 warnings ...

Guide to obtaining the current upload progress percentage using jQuery and Node.js

I am currently working on uploading a file using jquery, ajax, express, and nodejs. I am looking for a way to display the progress of the upload process. Perhaps there is a plugin or another method that can help with this. I do not need direct answers po ...

What is the best way to incorporate a style attribute into my EJS page content?

Having trouble with adding custom style. It seems to work, but there's an error displayed. Apologies for my poor english :( <div class="card card_applicant"> <div class="card_title" style="background-c ...

Encountered an error while running `npm init @eslint/config` and `npx create-react-app .`

When attempting to initialize an eslint config file for my node project using npm init @eslint/config, I encountered the following error. I've tried downgrading my node version, upgrading npm to the latest version, and clearing node cache, but nothing ...

Pulling information from MongoDB within a specified price range using Node.js

I am currently working on fetching data within a specified price range from the property model. For example, if a user provides a minimum and maximum price, I want to display the number of properties that fall within that price range. I have indexed the ...

Receiving warnings during npm installation and encountering difficulties with fixing issues using npm audit fix

Currently, I am working on developing an Angular application with a .NET Core Web API integration. Upon cloning the repository, I attempted to execute 'npm install' for the Angular application, but encountered an unexpected error: npm install n ...

Node.js Express application deployed but not receiving requests from React client application

I successfully deployed my project on Heroku. My server is built with Node.js + Express and the client uses ReactJS. To render the ReactJS page, I have included the following code: if (process.env.NODE_ENV === "production") { app.use(express.sta ...

The server is failing to provide the requested data in JSON format

I am struggling with making a simple API call using Node.js as the backend and React in the frontend. My Node.js file is not returning data in JSON format, and I'm unsure of the reason behind this issue. I need assistance with two main things: Why is ...

Is it possible to manually change the IP address in Universal Analytics?

I can't figure out where to specify the IP address override. This library mentions using ipOverride or uip, but it doesn't provide details on the exact location. "universal-analytics": "^0.4.20" Below is my code without overriding the IP addres ...

Transferring information from the front end to the backend route in React applications

I am new to using React and express, and I need to send data that I have collected from a form. The data I want to send back is the user's email stored in the state. However, I'm unsure of how to make this request. class ForgotPassword extends C ...

Axios and Express are throwing an error of "response is not defined

I'm facing an issue with the post method in my back-end code. Here's a simplified version of it: router.post('/users', async function(request, response) { try { const userToRegister = request.body; const user = await CreateUse ...

Encountering an issue retrieving static files through the Express static middleware

I'm having trouble with serving images on my backend. Here's the endpoint I've been trying to use, but with no success: app.use('/uploads', express.static(__dirname + '/uploads')); This is what "__dirname + '/uploa ...

Node.js - encountering difficulty loading environment variable in mysql2 module

I am currently facing an issue with utilizing the mysql2 package to connect node with MySQL. I have stored my username and password in a .env file, however, when I try to use it in my configuration for connecting to MySql, it does not seem to load properly ...