Adding fields from one collection to another collection in MongoDB based on specific conditions for a considerable amount of data

I encountered a situation where I constantly need to update a large number of collections.

Here are the collections:

coll1
{
  "identification_id" : String,
  "name" : String,
  "mobile_number" : Number,
  "location" : String,
  "user_properties" : [Mixed types],
  "profile_url" : String
}

coll2
{
  "identification_id": String,
  "user_id" : String,
  "name" : String,
  "mobile_number" : Number,
  "location" : String,
  "user_properties" : String,
  "profile_url": String,
  "qualified_user" : String,
  "user_interest_stage" :Number,
  "source" : String,
  "fb_id" : String,
  "comments":String
}

updated coll1
{
  "identification_id": String,
  "name" : String,
  "mobile_number" : Number,
  "location" : String,
  "user_properties" : String,
  "profile_url": String,
  "qualified_user" : String,
  "user_interest_stage" :Number,
  "source" : String,
  "fb_id" : String,
  "comments":String
}

For the collections coll1 and coll2, the following document insertion scenarios apply:

  1. If a user from coll1 is qualified based on certain scenarios, a record will be created in coll2.
  2. A new record can be manually created from API information in coll2
  3. The identification for coll1 in coll2 is the user_id
  4. There may be multiple records in coll2 for a single record in coll1

Now, we are merging these collections into a single collection, which will be coll1. We have decided to update qualified visitors using the key 'qualified_user' and update the corresponding user fields in coll1.

I have developed a script using Node JS and mongoose to fetch documents from coll1, verify a qualified_user in coll2, and update based on the following scenarios:

  1. If there is no qualified user, update the document with default values of an unqualified user
  2. If there is one qualified user, copy the qualification documents from coll2 and update in coll1
  3. If there are multiple qualified users, copy the first document and update in coll1. For the rest of the documents in coll2, create new documents in coll1
  4. After processing all documents from coll1, process coll2 documents that are qualified from APIs and create a new document in coll1

However, when running this script, I encountered the following error:

<--- JS stacktrace --->

==== JS stack trace =========================================

With a large number of documents in coll1, the processing time was significant. I used skip and limit to process all the documents, but it took 1 hour to complete. Is there a more efficient way to handle these types of database updates for a large number of collections?

Answer №1

Attempting to manage an excessive number of documents concurrently may deplete your available memory.

You are presented with two straightforward solutions:

  1. Utilize Mongo's cursor to sequentially process the outcomes rather than retrieving them all at once.
  2. Implement the --max-old-space-size parameter when executing your script, enabling you to manually define the script's memory allocation, like this:
    node --max-old-space-size=4096 script.js

Nevertheless, both approaches are not without flaws, especially considering that your data volume is likely to increase over time, rendering these methods ineffective. I would recommend reassessing your data structure. MongoDB, being a schema-less database, struggles with data redundancies. It is more advisable to store all data in a single collection and update specific fields under specific circumstances.

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

jQuery Algorithm for calculating totals

Here is an algorithm for formatting amounts using jQuery: Visit jsfiddle for the code However, there are some issues. For instance, if I enter 900.800,00 and then delete the "9", it still displays 00.800,00. How can this be resolved? I have fixed severa ...

A guide on extracting the text content from an anchor tag by using xPath() with a combination of selenium and Mocha

I have successfully chosen an <a> tag. My goal is to display the text of the anchor tag, but I am facing difficulties. The technologies being used are selenium, mocha, javascript, and phantomJS This is the detailed script: var assert = require(&ap ...

Having trouble passing a jQuery variable containing a string value to PHP through the jQuery AJAX function?

Below is the jQuery function code snippet: function processMessage() { if (textValue != "") { messageString='<div class="alert-box round"><p class="text-left">' + username + ':' + textValue + '</p>< ...

Mongoose Error: Schema Configuration Error - The specified type (`Bt`) is not valid for the `category` path

After taking a break from my project, I returned to find an error message that was previously unseen: 'TypeError: Invalid schema configuration: Bt is not a valid type at path category. See [site] for a list of valid schema types.' I'm puzzl ...

An addition operation in Javascript

Recently, I dipped my toes into the world of Javascript. However, I found myself puzzled by the script and its corresponding HTML output provided below. Script: <h1>JavaScript Variables</h1> <p id="demo"></p> <script> var ...

Determine the number of update queries executed in the past 24 hours within MongoDB collections

Discover the number of times an update query has been executed within the last 24 hours in MongoDB collections. How can I run a query to determine how many times an update query has been executed in the past 24 hours on the MongoDB collections? ...

Suggestions for improving the smoothness of the Bootstrap toggle hide/show feature

Recently completed my bootstrap toggle hide/show feature and everything seems to be functioning correctly, except for the transition between the different contents. The borders appear jagged and not smooth when activating the toggle. I suspect there may b ...

Problem: Implementing a horizontal scrolling feature using Skrollr

I'm interested in creating a horizontal animation controlled by skrollr. As I scroll down, I want the elements on my page to move from left to right within my container. When all elements have the same width, setting the scrolling data from 100% to 0 ...

Does CSS in the current IE9 beta allow for text shadows to be implemented?

Text shadows look great in Firefox and Chrome. For example, I have a basic website for streaming videos. Unfortunately, there are no shadows in IE9 for me. This is my CSS code: p { color: #000; text-shadow: 0px 1px 1px #fff; padding-bottom: 1em; } ...

While executing a jssor code in SP 2007, IE experiences freezing issues

I've integrated a jssor slider with vertical navigation (without jQuery) into a Sharepoint 2007 page using CEWP. All the image links have been replaced with images stored in the SP image library, and the jssor.slider.min.js file has been uploaded to t ...

A strategy for concealing the selected button within a class of buttons with Vanilla JS HTML and CSS

I have encountered a challenging situation where I am using JavaScript to render data from a data.json file into HTML. Everything seems to be functioning correctly, as the JSON data is being successfully rendered into HTML using a loop, resulting in multip ...

Creating elegant Select Dropdown Box in AngularJS without relying on images

I am currently working on an angular page and have implemented a dropdown with ng-Options to fetch and set values successfully. However, I am now looking to enhance the appearance of this dropdown. After exploring options like ui-select, I realized that be ...

Implementation of the I18next library

I am currently integrating react-i18next into my application to implement a switch button for French and English languages. Unfortunately, I am facing some issues as the translation is not working properly. I suspect it's related to the JSON file reco ...

Determine the identifier of the subsequent element located within the adjacent <div> using jQuery

I have a form with multiple input elements. While looping through some elements, I need to locate the id of the next element in the following div. You can find the complete code on jsfiddle $(":text[name^=sedan]").each(function(i){ var curTxtBox = $(thi ...

What steps do I need to take to set up a nodeJS worker to transfer data from mongo to elasticsearch using streaming

Currently, I am in the process of developing an application that relies on Change Data Capture (CDC) utilizing Mongo Change Streams to monitor for changes and promptly index them in elasticsearch. My progress so far includes implementing a worker that exe ...

Enable swipe functionality for mobile users

Looking to make the code below swipable on mobile devices. Any suggestions or resources to achieve this would be greatly appreciated! <script> var links = document.querySelectorAll(".heart"); var wrapper = document.querySelector("# ...

An issue with Axios request in a cordova app using a signed version

Currently, I am in the process of developing a Cordova application utilizing Axios and React. The interesting part is that everything runs smoothly when I build the app with Cordova and test it on my phone using the APK. However, once I sign, zipalign it, ...

Exploring various endpoints using firestore Cloud Function

I am in the process of creating a versatile Cloud Function that can be executed on various endpoints. This is how my original Cloud Function looks: const functions = require('firebase-functions') const admin = require('firebase-admin' ...

The variable is constantly reverting back to its initial value

Here is the code snippet: function send() { var nop = 6; var send_this = { nop: nop }; $.ajax({ type: "GET", data: send_this, url: "example.com", success: function(r) { ...

What is the best method for showcasing numerous dropdown lists using JavaScript along with conditional if-else statements?

Hello everyone, I am currently new to javascript and I'm attempting to build a small web application using javascript. However, I am facing an issue with printing the drop-down list output. Could someone please assist me in resolving this problem? < ...