Strategies for organizing libraries within mongoDB's system.js

Exploring techniques for storing prototype-based libraries or frameworks in mongoDB's system.js can be challenging. An issue arose when attempting to incorporate dateJS formats in a map-reduce function. JIRA #SERVER-770 clarifies that object closures, including their prototypes, are lost during serialization to the system.js collection, which is considered normal behavior. This limitation impacts popular frameworks like dojo, Google Closure, and jQuery.

Is there a workaround to store libraries without relying on prototyping? One potential approach involves initializing before Map-Reduce and passing them through the scope object but results have been mixed. If this method proves ineffective, what alternatives exist to facilitate server-side javascript reusability within mongoDB?

Answer №1

When utilizing JavaScript for queries, it is possible to either reuse an existing JS context or create a new one where stored JS objects are loaded. To achieve your desired functionality, you have two options:

  1. Have mongod automatically run the stored code upon installation
  2. Use mapreduce with an init method

The first option is more intriguing. Interestingly, the mongodb v8 build does this automatically (though unofficially), while the official spidermonkey build does not.

For example, if you store code like this:

db.system.js.save({ _id: "mylib", value: "myprint = function() { print('installed'); return 'installed';" }

In the v8 build, you can freely use myprint() in your code, but in the spidermonkey build, you would need to explicitly call mylib().

To work around this, you can create another method:

db.system.js.save({ _id: "installLib", value: "if (!libLoaded) mylib(); libLoaded = true;" }

And then call it from your map() function.

I have created a ticket to standardize engines and enable automatic execution:

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

Implementing server-side range filter in vue-tables-2: A step-by-step guide

I am currently using a server-side VueTables-2 component to display data from the database. The table includes columns with numeric, textual, and date values. My issue lies with filtering the numeric columns. I am trying to incorporate options for range f ...

sending ajax information to create a pie chart displaying data percentages

I am currently facing confusion on how to pass or assign a value of my data-percent pie chart through my ajax data retrieved from the database. While I have successfully passed other fields using an id, I am stuck on how to incorporate the value for data p ...

Is there a way to dynamically replace a section of a link with the current URL using JavaScript or jQuery?

I have a link that appears on multiple pages, and I want to dynamically change part of the link based on the current URL* of the page being visited. (*current URL refers to the web address shown in the browser's address bar) How can I use JavaScript ...

The callback function of jQuery getJSON would sometimes not be triggered

I've been struggling with this issue for hours now, and I can't seem to figure out why. Oddly enough, q seems to be functioning correctly. The URL returns a valid JSON response, displaying objects and arrays in the JSON tab under the Net-tab ...

Ways to terminate and fulfill a promise

Each time I execute this snippet of code, a promise is returned instead of the data being inserted into my database. Unfortunately, I am unable to utilize the await keyword due to it not being in a top-level function. Specifically, I receive the message: & ...

utilizing if statements to call upon the correct phases in collections

Below is a concept of an ideal syntax that I wish existed so I could easily fetch sessions from my Course collection. Each course in the collection has a sub-document array called sessions. I aim to retrieve courses based on user query parameters when sear ...

Facebook has broadened the scope of permissions for canvas applications

I am in the process of developing a Facebook canvas application that requires extended permissions for managing images (creating galleries and uploading images) as well as posting to a user's news feed. I am currently facing challenges with obtaining ...

Error in three.js: Attempting to access the 'rotation' property of an undefined object

As I try to animate a cube in my scene, I keep encountering an error that reads: "TypeError: Cannot read property 'rotation' of undefined." function animate() { requestAnimationFrame( animate ); render(); } ...

I'm having trouble accessing my POST data using console.log. Instead of getting the expected data, all I see in the console when I try to GET is "

My code for the POST function is working, but when I try to retrieve and display the POST response from the server, all I get is "null". Can anyone help me figure out how to properly send data from my form to the server and then successfully console log it ...

Encountering a 404 error while attempting to upload files with multer in node.js

I am currently developing a website using node.js where users can create small adverts with various information and upload images. I have successfully set up a mongodb database to store the data, but I am encountering a 404 error when trying to upload imag ...

Unlock the power of Angular pipes in window.open with URL parameters!

<div class="member-img" onclick="window.open(childEpisode.File_URL | fullPath)"> </div> The fullPath function concatenates the domain part to the relative URL stored in file_URL. However, there seems to be an issue as it i ...

You can activate the ability to click on the main tag by setting routerlink as the child component in Vue

Within my HTML code, I have utilized a RouterLink within an li tag to create a basic dropdown menu. Currently, when options are displayed in the dropdown menu, I am only able to click on the text itself to navigate to the next page. However, I would like t ...

Add motion to the div element when hovering and moving the mouse away

Looking to add animation to a list moving from left to right and vice versa. Now, I want the list to animate from left to right when the mouse hovers over the div, and then animate from right to left when the mouse leaves the div. Can someone assist me wit ...

What is the best way to structure Vue.js components for optimal organization?

Imagine having an index page called index.vue consisting of various components like this: index.vue <template> <div> <component-1 /> <section class="section-1"> <div class="container section-container"> <com ...

Effortless 'rotational' script

I am currently developing a HTML5 Canvas game with the help of JavaScript. My aim is to create an object that smoothly transitions to a specific direction. The direction is being stored as a variable and calculated in radians. Here's how the code op ...

Ways to refresh a canvas element without requiring a full page reload

My goal is to restart a canvas on a website without reloading the page, making the restart dynamic. However, I've noticed that after multiple restarts, the animation slows down and memory usage increases. The situation is complicated by my use of Met ...

What is the best way to eliminate excessive spacing between two containers in mobile view?

I have created a layout with a single container and a row split into two columns. The left column contains a title and the right column contains a card, both centered. I am facing an issue when viewing it on mobile, there is excessive spacing between the t ...

How many server queries does a single web application require?

As someone new to web app development, my main goal is to optimize speed as much as possible. I am faced with two options: The first option is to fetch data from the database individually every time a refresh is needed in the app. Alternatively, I c ...

How to extract and compare elements from an array using Typescript in Angular 6

I have created a new Angular component with the following code: import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { HttpClient } from '@angular/common/http'; @Compone ...

The XML content fails to display after initiating an AJAX request

Attempting to create an ajax call and accessing a field from the table on the server yields the following: <PushProperty><Status ID = "1"> Success </Status><ResponseID> b3633c9eeb13498f </ResponseID><ID> 9098 & ...