Saving additional user information within the users' data

I'm considering a method that might be questionable. I'm thinking of storing a user's likes under the profile attribute in my user object. Here's an example of what it would look like:

user = {
  ...
  profile: {
    likes: [
      {
        user: {
          _id: USER_ID,
          avatar: USER_AVATAR,
          username: USER_USERNAME
        }
        otherData: true
      },
      ...
    ]
  }
}

I have some reservations about this approach. One concern is that if the associated user changes their avatar, it wouldn't reflect the correct information for this user. Additionally, with an infinite amount of likes, I worry about the impact on performance as the array grows.

Is there a more efficient way to handle this? In a traditional database, I would create a separate table and use joins to retrieve a user's likes.

Cheers!

Answer №1

It's important to acknowledge the concerns you've raised about NOSQL databases and their potential data consistency issues stemming from storing data in multiple locations without proper normalization.

To address this, one approach is to only store userIDs in the likes array and then perform an application-level join to link profile likes with user data:

likes: [
{_id: USER_ID3},
{_id: USER_ID4}
]

However, implementing this method can regress back to a relational model, lacking SQL's advantages.

The best course of action will vary depending on your application's needs, considering factors like performance impact from joins or the risk of outdated data.

Answer №2

It is not recommended to store likes directly in the user collection as it will cause the user document to grow significantly.

To manage likes more efficiently, consider creating a separate collection specifically for likes. This way, you can store each like as a single document within the new collection.

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

Sending numerous arguments via HTTP DELETE request

In my code, I am trying to send the id and dept fields in a DELETE HTTP request and then retrieve them inside the deleteData() function. However, I am encountering an issue where the values for id and dept are coming up as null within the deleteData() fu ...

Delete a class that is not identified by the DOM

Currently, I'm developing an interactive map that triggers an overlay image to highlight the selected area. I am trying to dynamically add and remove classes from a div based on the highlighted area. Initially, I attempted using the starts with selec ...

Click to dynamically change the input number variable

I'm in the process of creating a special calculator, where you input the number of years into a field, hit submit, and see different results displayed below without the page reloading. All my calculations are working properly at the moment. However, ...

jquery sequential fade effect

I am trying to make each div fade in one by one. Visit this link for reference <div id="w"> <div class="one"></div> <div class="two"></div> <div class="three"></div> </div> $(function() { $(&a ...

Using trigonometry and Javascript to determine the necessary padding for an image to rotate without being cut off

Within the known rect (grey) of the outer parent element is an image element (orange) with its overflow hidden. I am trying to determine the necessary padding for the image element (with known width and height) in order to rotate it to a specific angle, s ...

Is there a way to utilize localStorage to retain a classlist toggle status for a light/dark mode theme switch on the browser?

I am currently working on a portfolio website that features a light/dark mode theme switch. The functionality of the switch is working properly, but it doesn't save the user's preference when they refresh the page or navigate to another section. ...

Targeting Multiple Ids with ModalPopUp Extender

Greetings everyone! Let's get started! So, I have a couple of buttons on my page and I want to use one modal popup for both save clicks. Here is the code for the buttons: <asp:Button ID="btnSave1" runat="server" OnClick="btnSave1_Click" Text="Sa ...

Specialized dropdown selection function not triggering upon click

I am facing a challenge in triggering an on-click function whenever a user selects an item from a dropdown menu. This is necessary because the default styling of the dropdown cannot be modified. My objective is to update a field with the selected item&apo ...

I am experiencing an issue wherein after using jquery's hover() function, the CSS :hover state is not

Following the jquery hover function, the css hover feature ceases to work. In my html, there are multiple svg elements. A jquery script is applied where hovering over a button at the bottom triggers all svg elements to change color to yellow (#b8aa85). S ...

Dynamic Text Labels in Treemap Visualizations with Echarts

Is it possible to adjust the text size dynamically based on the size of a box in a treemap label? I haven't been able to find a way to do this in the documentation without hardcoding it. Click here for more information label: { fontSize: 16 ...

Stopping Cross-Origin Resource Sharing (CORS) - Executing Ajax GET Requests on Ngin

I have set up Nginx as my web server and configured it as shown below: server { listen 3000; server_name .*; location / { proxy_pass http://127.0.0.1:3001; if ($request_method = 'OPTIONS') { add_h ...

Retrieve all the records from the collection that have a specific reference number in their ID field

Is it feasible to pull together all documents with an ID that includes a specific value? I am working with Angular 7. I attempted using db.collection('items').where.. but unfortunately, this method is not supported. For instance: (collection) ...

The jQuery validation applied to the data submission per post is not functioning as expected as the $_POST variable is appearing empty

I'm facing an issue with Jquery, AJAX, and field validation. Although the validation and redirect are working fine, I'm having trouble retrieving values from fields in the $_POST array. Below is the start tag of the form: <form class="smart ...

Initiate a click on a radio button while also retaining the selected option when the page is

This is a unique question. In my scenario, there are two radio buttons: "radio1" and "radio2." I have successfully implemented the following actions individually: Automatically triggering a click on "radio1" upon page load. This ensures that the button ...

Integrate an external script with React and initialize a new instance

I've been working on integrating a neat canvas background feature from this GitHub project into my React web application. Here's what I've attempted: import {WarpSpeed} from './warpspeed.js' import WarpSpeed from './warpspee ...

What is preventing me from installing socket.io?

I keep seeing an error in the console, what could be causing this? npm ERR! code 1 npm ERR! path E:\full-stack\proshop-2\socket\node_modules\utf-8-validate npm ERR! command failed npm ERR! command C:\WINDOWS\system32&bso ...

Is it possible for me to generate c3js graphs dynamically?

Here is my current progress: <div id="chart"></div> <script> var names = <?php echo json_encode($array1) ?>; var count = <?php echo json_encode($array2) ?>; var x=0; while (names[x]!=null) ...

update of the camera's close-range capabilities

Is there a way to adjust the near and far parameters of the camera without having to recreate it? I've noticed that the camera stops displaying objects when I attempt to make changes to these parameters. In some cases, it results in a black screen, ...

Enter your text in the box to search for relevant results

I need assistance with a code that allows me to copy input from a text box to the Google search box using a copy button. Upon pressing the Google search box button, it should display the search results. Here is the code I have been working on: http://jsf ...

The Array.find() method is not returning the desired value as it is not a recognized function

Is it permissible to use JavaScript's find() function on an AngularJS array? I am encountering some issues with this straightforward code snippet. The error message claims that the return value from $scope.names.find(name1) is not a function. TypeErr ...