What is the most effective method for preserving a record of information using MongoDB?

In my users collection, there is an attribute called "weight" that includes both a number value and a date. Users have the ability to update their weight, but I also want to store a historical record of all previous weights and the dates they were updated. I'm unsure of the best way to architect this functionality. Any suggestions on the optimal approach? My stack includes NestJS and Mongoose.

Answer №1

To determine the most suitable model for your application, it is important to consider the common queries used within it. More information about your specific application would be needed in order to recommend the optimal model. One suggestion would be to create a separate collection for weights, including fields such as userId, weight, and date.

This approach would be beneficial if there are times when you need to access user data without the weight history, as well as times when you specifically need to query the weight history for purposes like displaying weight evolution or calculating averages.

If user data and weight history are frequently queried together, embedding an array of weight documents within the users may be another option to consider. However, this method could result in more complex queries due to dealing with nested subdocuments in the array.

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

Not all generic types specified with an extends clause are appropriately narrowed down as expected

Consider the following scenario: type MyType = 'val1' | 'val2' | 'val3'; const variable = 'val1' as MyType; const val2 = 'val2'; const val3 = 'val3'; declare function test<U extends MyType&g ...

Connection severed with ('127.0.0.1', 57124) due to a broken pipe

Here is the code I've written for implementing a backend login functionality using django 1.10 and mongoengine 0.9.0 (MongoDB): @csrf_exempt def check_auth(request): csrf_dict = {} csrf_dict.update(csrf(request)) username = json.loads(reques ...

Fixing issue with delete and put requests not functioning properly after adding a new task

Currently delving into back end development and seeking assistance with mongodb and axios. Working on a basic todo app where tasks are stored in a mongoDB database. You can find the repository here. Encountering an issue where I cannot mark a task as do ...

Dealing with TS error - The target demands 2 elements, while the source might contain fewer items

As a newcomer to TS, I'm struggling to grasp why TS believes that Object.values(keyCodeToAxis[keyCode]) could potentially result in an array with less than 2 elements. type ControlKey = "KeyQ" | "KeyW" | "KeyE" | "Ke ...

Capable of generating numerous users using the same email address, even though the email has been defined as unique in Mongoose

Currently in the process of developing an API using Express, MongoDB, and Mongoose. Encountering an issue where multiple users can be created with the same email address, even though it should not be allowed. Despite having the 'unique: true' att ...

The functionality to save user likes in React is not properly functioning on the like button

I created a like button in React that saves my choices, but it seems to be not saving the choices of other users. Additionally, it doesn't appear to restrict only authenticated users from marking likes. Can someone please help me identify what I' ...

Using socket.io-client in Angular 4: A Step-by-Step Guide

I am attempting to establish a connection between my server side, which is PHP Laravel with Echo WebSocket, and Angular 4. I have attempted to use both ng2-socket-io via npm and laravel-echo via npm, but unfortunately neither were successful. If anyone h ...

How can Typescript elevate your use of the IFrame API?

Here is a code snippet that performs the following actions: let doc = this.iframe.contentDocument || this.iframe.contentWindow; let content = `...` doc!.open(); doc!.write(content); doc!.close(); Despite this, the Typescript linter thr ...

An issue arises when attempting to retrieve various Modules depending on the specific type of Module that is provided

My goal is to pass an array of different types to generate various combinations of submodules. However, I am currently only passing a single type which works fine. When I try to pass multiple types, the compilation fails. What steps can I take to resolve ...

Trouble persisting data with Mongoose on mLab

After numerous unsuccessful attempts to fix my logic, I have decided to seek assistance. I am currently developing an application using Node, Express, Passport, Mongoose, and MongoDB. At the moment, users can sign up and log in successfully. When a user s ...

"Mongoose Nodejs is throwing an error as it is unable to retrieve the property 'id' from an undefined

I'm currently attempting to retrieve and delete an object from MongoDB, but I keep encountering the following error. Cannot read property 'id' of undefined My goal is to fetch an object by its ID. The ID is crucial in my Schema as I am e ...

Encountering a CORS issue when trying to import a JavaScript file into my Vue.js web application

Having trouble calling an API from within my Vue.js instance's built-in methods I keep encountering some error message Currently in training and all of this is new to me Errors: Access to script at 'file:///home/wohlig/Documents/Projects/Trai ...

Combining arrays in Angular: A step-by-step guide

Below is the provided dataset: collection = [{ date: '2020-12-01', data: [{ id: 'A1', name: 'A1', date: '2020-12-01' },{ name: 'A2', date: '2020-12- ...

Problem encountered while performing npm installation on Ubuntu due to ENOENT error with lstat

I've been working on a project that handles blog posts and recently deployed it on a cloud platform without any issues. However, I decided to clean up my node_modules folder by deleting it and reinstalling using npm install. After doing so, I encount ...

How can we efficiently combine an array of ObjectId pairs with their corresponding collections?

In my course collection, I am assigning teachers to each subject of a specific course. The allotment information is saved as an array of JSON objects, you can refer to the documentation below for more details. { "_id" : ObjectId("5cc7d72d8e165005cbef93 ...

Warning from Vue Router: Issue with push/replace state - TypeError: history[(intermediate value)(intermediate value)(intermediate value)] is not a valid function

I've been working on setting up Vue Router (Vue 3 + Vue Router 4) and I'm facing some challenges with the following error messages: [Vue Router warn]: Error with push/replace State TypeError: history[(intermediate value)(intermediate value)(inter ...

Updating in bulk is taking longer than expected

Currently, I am utilizing pymongo to perform a bulk update process. The provided names list represents a distinct compilation of names, where each name could potentially correspond to multiple documents within the collection. Code 1: bulk = db.collecti ...

Traversing through CSV data and performing updates in MongoDB using Node.js

I am attempting to convert a CSV file to a JSON array using Node.js, and then update it in MongoDB. I am wondering if there is a way to iterate through the data and update the database based on the JSON using this code. If that's not possible, is ther ...

Error message: Deno package code encounters error due to the absence of 'window' definition

I am facing an issue with a npm package I imported into my Deno project. The code in the package contains a condition: if (typeof window === 'undefined') { throw new Error('Error initializing the sdk: window is undefined'); } Wheneve ...

Incorporating an additional ion-item alongside the existing one instead of substituting it

I am retrieving a list of questions from an API with pagination. I have a button that triggers a function to load the next page of questions. Instead of replacing the previous page, I want to append the new questions below the existing ones. Here is my cur ...