Revise the subarraylist within a meteor collection

Currently, I have both the game ID and player ID stored in session variables.

  Games.insert({
      board : hex_board(7),
      players : [{id: 0, hexIds: []}, {id: 1, hexIds: []}],
  });

At this point, I am facing a challenge:

  Games.update(Session.get("game"), {$addToSet: {players: ""}});

I'm unsure how to specify which element in the players list to target, but the condition is based on Session.get("activePlayer").

I almost have it working by hardcoding the key as 0 or 1; I just need to make it dynamic based on the activePlayer variable

        Games.update(Session.get("game"), {
            $addToSet: {
                "players." + Session.get("activePlayer") + ".hexIds": Session.get("selected_hex")
            }
        });

Solution implemented as a method

Meteor.methods({
  addHexIds: function(hexIds, player, game) {
    Games.update({_id: game, "players.id": player}, {
            $addToSet: {
                "players.$.hexIds": hexIds
            }
        });
  }
});

Answer №1

Maybe something along these lines...

Games.modify(
    { game_board : hexagon_board(7), "game_players.id": 0 },
    {
        "$add_to_set": { "game_players.$.hexIds": "bar" }
    }
)

You can customize as necessary

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

Find the YouTube URL that falls within a specific range and swap it with an iframe

Imagine you have the following HTML structure: <div class="comment"> [youtube]http://www.youtube.com/watch?v=videoid1[/youtube] random text </div> <div class="comment"> [youtube]http://youtu.be/videoid2[/youtube] random text2 </div> ...

Verification of the data entered in the input box

I am looking to develop an Input Box where users can enter the private details of a person. The first character must be either A or E, and the rest can be alphanumeric with no special characters allowed. I need to implement validation on the client side to ...

Utilizing data in mongoose: A beginner's guide

I have two database models: User and Conversations. The User model has the following schema: const userSchema = mongoose.Schema({ username: String, logo: String, ..... }) and the Conversation schema is as follows: const conversationSchema = mongo ...

Problems with Bootstrap affix scrolling in Internet Explorer and Firefox

I'm encountering an issue with the sidebar on my website. I've implemented Bootstrap affix to keep it fixed until the end of the page, where it should move up along with the rest of the content at a specific point... Everything seems to be worki ...

Tips for incorporating a last or recently visited feature similar to Google Docs using MongoDB

Within my database, I have two collections: users and projects. These collections share a many-to-many relationship. One user can be associated with multiple projects and one project can be assigned to multiple users. In the user object, I store an array o ...

What is the best way to automatically redirect to a different page once a video has finished playing after 10 seconds?

I'm working on creating an HTML page that will automatically redirect after 10 seconds once a video has finished playing. The issue I'm facing is that the page is currently redirecting as soon as it loads, rather than waiting for the video to fin ...

Use ajax to validate a form with jquery-validate

I encountered a problem while trying to update my data using PHP, Codeigniter, and AJAX. Although everything was working fine, I realized that I needed to validate my form with jquery-validate before making the AJAX request. I already have my validation ru ...

Embracing the node mindset with a Java foundation

As a Java Developer, I have become accustomed to working in a sequential manner where tasks are executed one after the other or concurrently with multiple threads. The organization of code in a sequential way seemed logical under this paradigm. However, wh ...

Tips for successfully sending arrays of strings to a different function in C

I've encountered some difficulty in transferring the randomly generated strings from one function to another. I specifically aim to pass the strings created by the inputAccounts() function to the viewAllRecords() function. These strings, produced by ...

Display the checkbox as selected using AngularJS

I have already submitted a form with a checkbox value. Now, I am trying to display the checkbox as "checked" using AngularJS based on the value retrieved from the data source. Can someone guide me on how to achieve this? ...

Rotating a div in HTML and CSS is not achieving the desired 180-degree rotation

Having trouble implementing a Fill animation that goes from the bottom to the top of the box .box-inner-1. The CSS Rotate property seems to be malfunctioning! $('.box-inner-1').css({ top: '0' }).animate({ "height": 260 }, 4000); ...

Utilizing Jquery on the client side in conjunction with a Node.js server

I am using nodejs within a docker-compose container to create a local web app. My goal is to use jquery on the client-side to load an HTML file into a div, but I'm encountering issues with it not working properly. Both the initial index.html and the n ...

Having trouble with the rendering of the Stripe Element Quickstart example

Currently, I am diving into the world of Stripe's Element Quickstart. Take a look at this fiddle that I have been working on. It seems to be quite different from the example provided. Although I have included the file, I can't seem to figure out ...

Is there a way to dynamically change the source of an image based on different screen sizes using Tailwind CSS?

Currently, I am utilizing the next/Image component for setting an image and applying styling through tailwindcss. However, I have encountered a roadblock at this juncture. My Objective My goal is to dynamically change the src attribute of the image based ...

Angularjs detects directives on the newly created DOM

Greetings! I've been hard at work on my Angular app and have a question. Does AngularJS detect directives when a new DOM is added? Let's imagine a scenario where I have an Angular directive called mention that compiles when the DOM contains a cl ...

Retrieve the BSONObjectID of the newly added document

I am looking to retrieve the newly inserted object. I have come across information suggesting I can create my own ID, but that may not be the best approach. def create(repo: String) = Action.async(parse.json) { implicit req => val id = BSONObjectID ...

Retrieve data from a JSON object and assign it to a variable in JavaScript

I'm currently working on implementing AJAX to send and receive data in Django. My model consists of three fields: id, parent, and text. However, when attempting to post the information back to Django, I encounter an error due to additional fields pre ...

What is the best way to retrieve the exact matched field values in MongoDB?

Below is a table I have created after writing the code for it. Please use organization db.test.insert({ bookname : "Mongodb", author : "Alex", price : 45, qty : 100}) db.test.insert({ bookname : "Cassandra", author : "John", price : ...

Implementing GRIDFS for mp3 storage in a meteor application

Being a novice in the world of meteor, I am currently working on defining an mp3 collection and then uploading music to it from the admin page. The packages that are installed include: cfs:standard-packages cfs:gridfs cfs:filesystem 1) I have successful ...

having difficulty retrieving information from JSON in order to make comparisons

Here is a fiddle I created to learn jquery with json here. I am attempting to create a demo app similar to this. I am struggling with how to calculate currency values by retrieving data from the localStorage. Can someone guide me on the steps to achieve t ...