Tips for accessing the data from an object's num property using the name property as a selector

I am attempting to locate the value of the num property within this object by using the name property to identify the correct object from the hashtagsl array; thus, obtaining the correct num value. I need to accomplish this task with only the information of the name within the element and the user ID. The ultimate objective is to assign a variable to the value of the num in the specific object. Below is the code that generates the object to display its structure. Thank you in advance.

All code displayed is executed on the server side.

Meteor.users.update({"_id": this.userId},{"$push":{"profile.hashtagsl": {name: newhashtag, num: 1}}})

edit

The following code snippet updates the num property by incrementing it. My goal is to retrieve the value of num without updating it.

Meteor.users.update({"_id": this.userId, "profile.hashtagsl.name":hashi},{"$inc":{"profile.hashtagsl.$.num":1}});

Answer №1

Here is a solution that should work:

const user = Meteor.users.findOne({'profile.hashtags1.name': hashtag}).profile.hashtags1[index].num;

When working with nested properties in a MongoDB query, you can use dot notation to access them. The returned object from findOne will have a similar structure, allowing you to retrieve the desired property just like any other javascript object.

Don't forget to specify the index when accessing the hashtags, as the user document holds an array of them.

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

Repeating items must be unique; duplicates are not permitted on ng-repeat

The data retrieved from the service request is in JSON format and looks like this: { "entries": [{ "id": 2081, "name": "BM", "niceName": "bodmas" }] }, { "id": 8029, "name": "Mas", "niceName" ...

Tips for overriding ng-disable in AngularJSUnleashing the

This is a comment box using AngularJS: <tr> <td colspan="5"><br/>Comments* <div><textarea class="form-control" rows="3" cols="60" ng-model="final_data.drc_scope" placeholder="Add comments here" ng-disabled="is_team==0 || isDis ...

Tips for properly setting mongoose indexes and verifying their functionality

Is there a way to test if the indexes I set are actually working? Can I check using Node.js during development or do I need to use the mongo shell? I noticed indexes being created in mongoDb Compass, but I am using mongoDb Atlas. Do I need to set the index ...

Tips on Including Static Header and Footer in Multiple HTML Pages

What is the best way to reuse a header and footer on multiple HTML pages without repeating code? I have 5 HTML pages that all need to include the same header and footer elements. How can I efficiently reuse these components across all pages? ...

Using jQuery to target the element before

Is there a way to determine the width of elements located before an element when it is hovered over? I attempted to achieve this using the following code: $('ul li').hover(function() { $(this).prevAll().each(function() { var margin = $(this ...

React components do not re-render when the context value changes

The issue with React not re-rendering when the context value changes persists I am using tailwindcss, React(v18.2.0), and vite(3.2.4). I have already attempted i want that clicking on TodoItem should change the completed value in the todo using React con ...

What is the best method for combining multiple text box values into a single text box?

I need assistance with concatenating multiple text box values into one dynamic text box. I am currently retrieving values using IDs, which are dynamically added as the text boxes are dynamically created. However, when using a for loop to dynamically add te ...

When using Vue.js, binding may not function properly if it is updated using jQuery

Link to JsFiddle Below is the HTML code: <div id="testVue"> <input id="test" v-model="testModel"/> <button @click="clickMe()">Click me</button> <button @click="showValue()">Show value</button> </div& ...

Vanilla JavaScript Troubleshooting: Top Scroll Button Issue

Attempting to develop a Scroll To Top Button utilizing Vanilla JS, encountering errors in the dev console. Existing jQuery code that needs conversion to vanilla js Uncaught TypeError: Cannot read property 'addEventListener' of null My Vanilla ...

The Fusion of Ember.js and Socket.io: Revolutionizing Web

I have been trying to incorporate a basic Ember application into the view of my node application. I have verified that Ember is properly set up and my sockets are functioning correctly. However, I am encountering an issue where the data is not being displa ...

Utilize AJAX within an Angular method

I am encountering 2 issues with the $http function in this particular code snippet. $scope.buttonClick = function(){ // Sending input data $.post('lib/add_contact.php', $scope.contact, function(data){ data = angular.fromJ ...

The Angular component router-outlet is not recognized as a known element

I have encountered an error message: 'router-outlet' is not a known element: 1. If 'router-outlet' is an Angular component, then verify that it is part of this module. 2. If 'router-outlet' is a Web Component then add ...

What purpose does the class serve in typescript?

This is a unique version of app.component.ts in the Angular Tour of Hero tutorial. import { Component } from '@angular/core'; export class Superhero{ name : string; id : number; } const SUPERHEROES : Superhero[] = [ {name : 'Wonder ...

Adding options to a dropdown menu dynamically while editing a form with the help of javascript

I have a dropdown like in below:- <form name="depositForm" action="<?php echo site_url('ajax_funds/deposit_funds'); ?>" id="depositForm" class="page-form-main form-horizontal " autocomplete="off" method="post"> <div class="form- ...

Can a base64-encoded image be saved as a string in a JSON and then transmitted as the payload for a POST request?

I needed to store a user document with photos taken from a react-native app image picker in a MongoDB database. My server is built using Express. Here's the scenario: const res = await fetch('https://myServerUrl', { method: 'POST&a ...

Angular examine phrases barring the inclusion of statuses within parentheses

I require some assistance. Essentially, there is a master list (arrList) and a selected list (selectedArr). I am comparing the 'id' and 'name' from the master list to those in the selected list, and then checking if they match to determ ...

Using a function as a condition within the find method in Express and MongoDB

I am a restaurant owner with a list of restaurants, and I am looking to filter out restaurants that are within a certain distance X from the user's location. Each restaurant has its latitude and longitude coordinates (referred to as lat and lon) while ...

How do I incorporate an external template in Mustache.js?

Welcome, I am a beginner in using Mustache.js. Below is the template and JS code that I have: var template = $('#pageTpl').html(); var html = Mustache.to_html(template, data); $('#sampleArea').html(html); Here is the template ...

Silky animations in kinetic.js (html5 canvas)

I am seeking a better grasp on kinetic.js animation. Recently, I came across a tutorial at . After experimenting with the code provided, I managed to create an animation that positions my rectangle at x coordinate 100. However, I am struggling to achieve s ...

Updating values in MongoDB with data from a DataFrame

I have an example of a dataframe with the following information: query new_query 0 Whats the time What's the time? 1 its late it's late! Next, I have a mongodb collection structured like this: { ' ...