Navigating a group of records in MongoDB

As a newcomer to MongoDB, I've set up two collections: "groups" for group information and "groupUsers" to establish user-group relationships.

Currently, my goal is to retrieve all groups that the current user belongs to and then store this information in an array. However, I'm facing an issue where only the first or last document is being added to the array, despite knowing that the user is associated with 2 groups.

This is how I am attempting to achieve it:

var playerID = Spark.getPlayer().getPlayerId();

var dataList = Spark.runtimeCollection('groupUsers');
var myGroups = dataList.find({ user: playerID });
var groups = [];

if( myGroups.hasNext() ) {
    var dataList2 = Spark.runtimeCollection('groups');
    var obj = myGroups.next();

    var thisGroup = dataList2.find({ "_id": {"$oid": ""+obj.group}});

    groups.push(thisGroup);
}

Spark.setScriptData("myUserGroups", groups);

I hope this explanation is clear enough. Any assistance on what may be going wrong or pointing me in the right direction would be greatly appreciated :-)

Thank you in advance.

Answer №1

Shouldn't you run a loop to fetch

dataList2.find({ "_id": {"$oid": ""+obj.group}});
multiple times if your code
var myGroups = dataList.find({ user: playerID })
returns 2 groups?

It looks like you are only executing it once.

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

When attempting to evaluate JSON data on a specific computer, the function JSON

Something strange is happening and I can't seem to figure it out, causing a big issue for me. I am currently working on a .Net web application that utilizes JSON (not json2) along with other JS libraries. In one specific proxy, the function JSON.eval ...

When the onclick event is triggered, the return value will execute after the onclient

After my onclientclick event finishes, I am trying to run my onclick event. However, regardless of whether my checkbox is selected or not, the program keeps prompting me to "Please select at least one to delete." Can anyone help me figure out why this is h ...

I can't seem to figure out why I continue to receive the error message stating that 'app.get is not a function'

Below is the code I am currently using: const request = require('request'); const app = require('express'); app.get('/', function (req, res) { res.send('hello world'); }); app.listen(3000); Unfortunately, I keep e ...

Enhancing Performance by Optimizing Module Loading for Frontend Component Rendering in Next.js

When building a Nextjs app, it is common to use the same package across multiple components on a page. However, in the case of client-side rendering, the default behavior does not optimize the loading of the common package. This can result in a significant ...

Thirteen consecutive attempts to fetch information have resulted in failure

I've encountered an issue while attempting to fetch data from my .NET Core 7 API. The error message I'm receiving is as follows: *Unhandled Runtime Error Error: fetch failed Call Stack Object.fetch node:internal/deps/undici/undici (11413:11) pro ...

What is the mechanism behind the operation of the inherits feature in Node.js?

The following code snippet illustrates how the inherits function works in node.js: exports.inherits = function(ctor, superCtor) { ctor.super_ = superCtor; ctor.prototype = Object.create(superCtor.prototype, { constructor: { value: ctor, ...

Implement a hover effect on a specific class targeting a nested element using JavaScript

Is there a method to apply a class that modifies rules for a child element using Javascript? I have successfully added it with CSS but am struggling to do the same with Javascript. Removing the class in JS is easy by referencing the classname before the ho ...

The Gulp task is stuck in an endless cycle

I've set up a gulp task to copy all HTML files from a source folder to a destination folder. HTML Gulp Task var gulp = require('gulp'); module.exports = function() { return gulp.src('./client2/angularts/**/*.html') .pipe( ...

A function that handles HTTP request and response

In order to decrypt data, I need to retrieve the encrypted response by hitting a specific URL and then use that encrypted data along with a key to decrypt it. Initially, I attempted to fetch the data response using POSTMAN, but all I got back was a series ...

Having trouble reaching the JSON data, resorting to utilizing variables instead

I attempted to retrieve information regarding the status of certain buttons in JSON format using the $.getJson() method. The ID with a dash has been split into two parts and stored in an array regarr. However, I am unable to retrieve the data from JSON usi ...

Experiencing excessive delay in loading data into the DOM following a successful response from a service in AngularJS

I am facing a challenge in loading a large set of data into an HTML table. To begin, you need to click on a button: <a ng-click="get_product()">load data</a> This button triggers a function called get_product: $scope.get_product = func ...

Transmitting intricate Javascript Array to ASP.NET Controller Function

I am facing an issue with sending a complex JavaScript array to my asp.net mvc6 controller method. I have tried two different methods to pass the data, but neither seem to be working for me. public IActionResult TakeComplexArray(IList<ComplexArrayInfo ...

Tips for automatically activating the HTML select menu on an iPad

My website features a suburb lookup tool that allows users to input a suburb or postcode (Australian only, e.g. 4000, 2000, Brisbane, Sydney etc) and receive the corresponding suburb/state/postcode in a select menu format: <select size="4" name="contac ...

Is It Possible to Save Data to Local Storage Using Vue.js?

I am currently working on storing data using Vue.js and local storage. By writing localStorage.set('something', 5) in my main.js file, I can view the data in the Chrome dev tools under the 'Storage' section in the 'Application&apo ...

Activate CSS element with a click

Is there a way to add a click event to a CSS class so that when it is clicked, it changes to a different class? I am specifically looking to change the character class on li items when they are clicked. Here is the code snippet: <!DOCTYPE html> <h ...

I have an Observable but I need to convert it into a String

Seeking assistance with Angular translation service and Kendo.UI components. In the Kendo.UI documentation, it mentions the use of MessageService for component translation implementation. To achieve this, an abstract class must be extended containing a m ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

Issue encountered when attempting to add multiple JavaScript functions through Greasemonkey using unsafeWindow

One issue that arises is that the functions are unable to recognize each other. For example: unsafeWindow.helloworld = function() { alert('Hello world!'); helloworld2(); // this fails with an error indicating it does not exist } unsa ...

Prevent scrolling after every time the mouse wheel is used

I have created a function that increments a class on a list item, here is the code snippet: var scrollable = $('ul li').length - 1, count = 0; $('body').bind('mousewheel', function(e) { if (e.originalEvent.wheelDelta / ...

How to insert a span element within a link tag in Next.js/React.js

Looking to create a breadcrumb using microdata in a Next.js and React.js project. Initially, I tried the following: <li itemProp="itemListElement" itemScope itemType="https://schema.org/ListItem"> <Link href={'/index&a ...