Exploring revisions within the MongoDB database

Currently, I am in the process of designing a MongoDB database to interact with a script that periodically polls a resource and stores the response in the database. The existing structure includes a collection with four fields: id, name, timestamp, and data.

My goal is to identify which names experienced changes in the data field between script runs and which did not. Here's a pseudocode representation:

if(data[name][timestamp]==data[name][timestamp+1]) //data has not changed
store data in collection 1
else //data has changed between script runs for this name
store data in collection 2

I am seeking a query solution that can achieve this without needing to iterate through and execute JavaScript logic on each item within the collection. With millions of documents present, such an approach would be inefficient.

Would it be beneficial to create a new collection named timestamp each time the script runs? Could this potentially improve speed and organization? Are there alternative schema designs that could prove more effective?

The script operates once daily so namespace limitations are not expected to pose an immediate concern.

Answer №1

Alright, here's an interesting question because essentially you need to iterate and execute JavaScript on each item.

What makes this intriguing is that it's not all that different from what an SQL solution would have to go through. Essentially, you're performing a self-join where x.1=x.1 and y.1=y.2. Even if the relational database can handle this operation, it won't be efficient with a large number of entries.

So, you're on the right track. Here are some additional tips to streamline the process:

  1. Make sure you have an index on Name/Timestamp for quick lookups.
  2. Execute a db.mycollection.find().foreach() loop over the dataset.
  3. For each entry, you'll need to a) Compare, b) Save appropriately, and c) Update a flag to show that the record has been processed.
  4. For future loads, you can add a query to your find statement:
    db.mycollection.find({flag:{$exists:false}}).foreach()
  5. Utilize db.eval() to improve performance.

The reason behind the "Name/Timestamp" index is to speed up the search for each "successor."

The use of the "processed" flag ensures that you don't re-process the same item multiple times. If you find timestamp 'n' followed by 'n+1', that should be the only 'n+1' instance.

If you're running this process once a day, chances are that it will perform adequately, especially if you're only dealing with new records. Just anticipate that it might take a few minutes.

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

Unable to successfully import data from vue using vue-chartjs

Encountering an error in my Vue 2 project after compilation: Failed to compile. ./node_modules/vue-chartjs/dist/index.js 85:11-26 "export 'defineComponent' was not found in 'vue' The specific line in the above file triggering thi ...

Add the item to a fresh array using the Ajax function

Here is an array that I have: var arrayOfResults = []; // Results after like statement After making a database call, I receive a JSON result like this: [{ "id": "{fcb42c9c-3617-4048-b2a0-2600775a4c34}", "pid": "{34214CCB-90C3-4D ...

The stylesheet is linked, but no changes are taking effect

I've linked my template.php to style.css, and when I make changes in template.php, they show up fine. However, if I make changes in the stylesheet, nothing happens. What should I do? My website is not live; I'm working on it using a WAMP server. ...

Chokidar operates smoothly from the command line, but fails to function properly when invoked through the use of 'npm run'

I've implemented a script that monitors Tailwind CSS files using Chokidar. The script works perfectly when I execute the command in the CLI using chokidar 'tailwind.config.js' --initial=true -c 'npm run build-tailwind'. It successf ...

My MongoDB script is experiencing difficulties inserting all the data

I am working with a continuous stream of /r/n terminated data that I need to insert into a mongodb database. tag~4~keyword~sim tag~5~keyword~mib tag~4~keyword~gom tag~3~keyword~qbo tag~6~keyword~qqq tag~3~keyword~k94 tag~4~keyword~g93 To process the data ...

Encountering "Error: Unable to access properties of null (reading 'isCE')" issue during upgrade from version 3.2.37 to 3.2.39 of Vue

After upgrading Vue in my project from 3.2.37 to 3.2.39, running Jest now results in the following error: [UnhandledPromiseRejection: This error occurred due to either throwing inside an async function without a catch block, or rejecting a promise that was ...

Tips for saving JavaScript results to a form

Hey there! I'm looking to figure out how to save a JavaScript calculation within a form instead of just displaying an alert or outputting it on another page. Below is the JavaScript code I have for calculating prices. <script type="text/javascript ...

Angular ngClass and ngIf directives failing to update upon alterations

In my current Angular project, I am working on a functionality where I need to dynamically change a class based on a variable without having to refresh the page. I have experimented with *ngIf/else and [ngClass] directives, which do work, but unfortunatel ...

How to Specify ContentType for a New Window in JavaScript after Submitting MVC Form

When a link is clicked, I want to open a new window with content determined by a post to my MVC controller. Here's how I currently approach it: jQuery.ajax({ type: "POST", url: '/controller/mycontroller', data: { myd ...

Efficient PHP file upload using Ajax

Where should I place my PHP SQL query to insert image information into my database? I attempted to put it just before the echo "success", but it did not work. <!-- Upload Button--> <div id="upload" >Upload File</div><span id="status" ...

Converting a class into a cohesive JSON string within ASP.NET MVC

I have the following class: [Serializable] public class ApiRequestStatus :IEquatable<ApiRequestStatus> { public static readonly ApiRequestStatus Failure = new ApiRequestStatus("Failure"); public st ...

Using JavaScript to load content with AJAX on skip links

Is there a way to prevent the error "loadWithAjax is not defined" from occurring while using the script below? addEventListener('click', function (ev) { if (ev.target.classList.contains('bpb')) { ev.preventDefault(); ...

Removing the dollar sign and retaining only the numerical values in an array using JavaScript

Using the code snippet below, I am attempting to retrieve elements by their class name in JavaScript and save them in an array. This is the current output I receive: "I received this output from the initial prices array $30.00, $20.00, $40.00" My inquiry ...

ngSelect fails to load ajax data automatically upon page initialization

In my angular web application, I have a select element that is populated with data from an AJAX call. The issue I am facing is that the options in the select are not displayed until the select box is clicked on and then unfocused. Only after this action, a ...

Unable to download and install jspdf version 1.5.3

Currently, I am facing an issue where I need to convert HTML to PDF using jspdf 1.5.2. However, I am encountering an error that says "Cannot read property 'charAt' of undefined" when trying to utilize html2canvas. When attempting to upgrade to j ...

MongoDB's approach to mimicking the SQL 'UNION ALL' functionality

Consider the existence of two distinct collections: Sales { "_id" : ObjectId("5ba0bfb8d1acdc0de716e839"), "invoiceNumber" : 1, "saleDate" : ISODate("2018-09-01T00:00:00.000Z"), "totalTaxAmount" : 613, "subTotalAmount" : 2000, "totalAmount" : ...

What is the best way to integrate ngx-translate's pipe for global translation?

I'm currently utilizing the ngx-translate package in order to internationalize my Angular application. When translating text using the translate pipe like so: {{ 'title' | translate }} An issue arises when attempting to use this pipe in ot ...

Attempting to reveal or conceal the change_list filter within Django admin

Is there a way to hide the filter box (the grey box on the right) on the django admin change_list.html page? I attempted to create a basic JavaScript function and insert it into the extra head section like this: {% extends "admin/change_list.html" %} {% ...

Does NextJS come with an index.html template page pre-installed?

Today, I delved into NextJS for the first time as I transitioned a ReactJS website over to it. While I find it to be a powerful framework, there is one particular feature that seems to be missing. In traditional ReactJS (without the NextJS framework), we ...

The KML file cannot be read by the Google Maps KML Layer

I'm currently facing an issue with reading a simple KML file using Google Maps API 3. I have created the KML file in Google Maps Editor and I am using the google sample codes, but I can't seem to identify where the problem lies. gm-sample.html ...