Tips on how to modify database records rather than generating new ones

Currently, my team is developing a web application that utilizes wearables to monitor vital parameters. As part of our integration testing, we are using a Fitbit device. The app itself is built with Angular and JavaScript, while the database is hosted on Cloudant. I am seeking advice on how to maintain updated database entries in a general sense. Specifically, I would like to gather wearable data every two hours and update an existing entry for the same day instead of creating duplicates.

At this point, I have considered two potential solutions:

  1. Storing today's data, including the entry's ID, in a variable to be updated periodically, resetting it at midnight.
  2. Prior to each update, retrieving all entries and checking for one matching today's date. If found, updating that existing entry; otherwise, creating a new one.

I'm not entirely satisfied with these options and would appreciate any guidance or alternative approaches.

Thank you in advance.

Answer №1

When working with Cloudant/CouchDB, updating existing documents can lead to conflicts. A better approach for scenarios like "IoT" applications is to create a new document for each sample and use a view to organize the data. Your documents could follow this structure:

{
   "timestamp": "2016-12-01T13:25:02.000Z",
   "type": "pressure",
   "value": 110.0
}

To materialize the data, you can utilize a design document that emits the data in a specific format:

function(doc) {
    if (doc && doc.timestamp && doc.value && doc.type) {
        var ts = Date.parse(doc.timestamp);
        emit([doc.type, ts], doc.value);
    }
}

For more information, check out:

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

watchify with gulp requires two saves to update modifications

For a while now, I've been experimenting with watchify and encountering a saving issue. It appears that every time I make a change, I have to save twice for the modifications to reflect in the output. If I add new code to any JavaScript file, the ch ...

Purging data when no input is detected in React.js

I need to figure out a reliable way to detect when my input field has been cleared so that I can clear the associated data. Currently, I am using the logic if (searchTerm.length === 0) to check for a cleared input. However, I have also experimented with i ...

I'm looking for recommendations on database management systems that are compatible with Node.js. Can

Could you recommend a reliable database that is compatible with node.js? I am currently using node webkit and need a robust DBMS for the backend. Any suggestions on plugins that may have external dependencies would be greatly appreciated. ...

Can a key event be activated on the DOM Window using Javascript or JQuery?

Can a key event be triggered on the DOMWindow or DOMDocument using JavaScript? I am developing a browser extension to interact with a website that has shortcut key events, similar to those in GMail. While I have come across other methods for triggering key ...

Understanding the extent of testing coverage in a project using karma and webpack

For my project, I am incorporating ES6 syntax and testing my code with Karma. While I have successfully set up testing, I encountered an issue with the coverage report. Instead of including the source code, the coverage report is highlighting spec file ...

Give GetElementsByClassName a shot

Hey, have you tried using js ref_doc_getelementsbyClassName? "I keep getting the error message 'Uncaught TypeError: Cannot set property 'value' of null' " Check out this HTML code snippet: <input type="text" cla ...

Navigating with Vue Router in a Nuxt JS vuex Store

In my app, I'm currently developing a login/register system using Firebase and Vuex for authentication and database management. Within Nuxt JS, I have implemented actions to create a user, log them in, and sign them out. After a successful registrati ...

What is the best way to determine when the context value has been established?

Currently encountering an issue while trying to display a header. To achieve this, I begin by making an API call in InnerList.js and setting a list in context using the data from the API call. Next, in Context.js, I assign the list to a specific data set ...

Using Selenium WebDriver in JavaScript to Extract Text from an Array

During my experimentation with Selenium webdriver in javacript, I encountered a challenge when trying to extract text from an array of WebElements (specifically cells within a table). The usual command getText() did not work as expected when attempting to ...

The Keyup Filter in the FromEvent function is malfunctioning and not behaving as anticipated

I have created a simple search function for my app using the FromEvent KeyUp and debounceTime features as shown in the code below: <input matInput #inputSearch> @ViewChild('inputSearch', { static: false }) input: ElementRef; fromEvent(th ...

The error message popping up reads as follows: "TypeError: _this2.setState cannot

I am encountering an error that I don't quite understand. How can I resolve this issue and why is it occurring in the first place? Although I am receiving the correct response from the API, I am also getting an error immediately after making a call. ...

Using jQuery to handle multiple AJAX XML requests

Currently, I am working on developing a JavaScript XML parser using jQuery. The idea is that the parser will receive an XML file containing information along with multiple links to other XML files. As the parser runs, it will identify tags within the file ...

Bring in typings from a package with an alternate title

I have a project that I am currently converting to typescript. In this project, I am using the package ng-idle. Additionally, there is a corresponding @types package named angular-idle, which contains the file @types/angular-idle/index.d.ts with the follow ...

Understanding Restangular with Typescript can be challenging, especially when dealing with a 'restangularized' response object

In my project, I am working with Angular 1.5.x using TypeScript in combination with restangular to access a remote API. Here is an overview of the scenario: The API endpoint I am connecting to is http://localhost:53384/api/timezones. Making a GET request ...

Transitioning from Jquery to vanilla JavaScript or transforming Jquery code into pseudo code

Struggling with a snippet of jQuery code here. While I have a good grasp on JavaScript, my knowledge of jQuery is somewhat limited. I am seeking assistance to analyze the following code in order to translate it into plain JavaScript or pseudo code that ca ...

When AJAX is invoked, the HTML content fails to display within a div

The following is the ajax script I am currently utilizing: mypage.php $(".sales_anchor").click(function(e) { e.preventDefault(); var store_username = $("#storeUsername").val(); var store_id = $("#storeID").val(); var sale_id = $(this).a ...

Making changes to an AngularJS property updates the value of an HTML attribute

One of my pages, base.html, contains the following code: <html lang="en" data-ng-app="MyApp" data-ng-controller="MyController"> <body style="background-color: [[ BackgroundPrimaryColor ]]"> . . . {{ block ...

Animating a background image to slide in from the bottom to the top using CSS transition

Here is a link to a codepen example: https://codepen.io/jon424/pen/XWzGNLe The current effect in this example involves covering an image with a white square, moving from top to bottom when the "toggle" button is clicked. I am interested in reversing this ...

merge two structures to create a unified entity

I have been searching in vain, can you please advise me on how to combine these two simple forms into one? I want it to be like a box with a select option and a button. The challenge for me is merging the two different actions, ".asp", into one script fo ...

Embed script tags into static HTML files served by a Node.js server

Looking to set up a Node server where users can request multiple pages from a static folder, and have the server inject a custom tag before serving them. Has anyone had success doing this? I've tried with http-proxy without success - not sure if a pro ...