Meteor: Form a fresh collection using a single attribute from a current collection

In my Meteor app, I have a collection

fullList = new Mongo.Collection('fullList');
. This collection consists of an array of objects with attributes like Color, Factor, and Tot.

My goal is to create a new collection or array that specifically contains all the Tot values from the existing collection. The pseudo-code for this would be something like newList = fullList.Color.

While I can display one attribute in HTML using {{Color}}, I am struggling to manipulate it in JavaScript.

The reason behind creating this new array is to utilize D3.js for visualizing the data.

Answer №1

It appears that your collection consists of serialized objects, rather than a single-document collection storing an array. In this scenario, you can utilize the map function built into your collection cursor. Details can be found in the documentation linked below:

To implement this, you can do the following (utilizing only the document argument in the callback):

fullList = new Mongo.Collection('fullList');
newlist = fullList.find().map(function(document) {
  return document.Tot;
});

The map() method will cycle through all documents within the collection - since no arguments are specified for find() - and for each document, it will append an element to an array (stored in newList) based on the value returned by the callback function, such as Tot.

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

Default values in MongoDB are not stored; instead, they are calculated again at runtime

Currently, I am in the process of developing a straightforward REST application using the Yeoman Express MVC generator integrated with MongoDB. Below is my MongoDB/Mongoose model (upgraded with complete update.js model): var mongoose = require('mong ...

Can the hasClass() function be applied to querySelectorAll() in JavaScript?

As a beginner in javascript and jquery, I recently attempted to manipulate classes using the following code snippet: if ($(".head").hasClass("collapsed")) { $(".fas").addClass("fa-chevron-down"); $(".fas&qu ...

JavaScript encountered an abrupt cessation of input, catching us off guard

Can someone please help me identify the issue with the JavaScript code below? I encountered an error message stating "Unexpected end of input", but upon reviewing the code, I couldn't find any apparent errors. All my statements seem to be properly ter ...

Tips on organizing a two-dimensional array based on the values in a specific column?

I could use some assistance with sorting a 2D array in JavaScript. The array will be structured like this: [12, AAA] [58, BBB] [28, CCC] [18, DDD] After sorting, it should appear like this: [12, AAA] [18, DDD] [28, CCC] [58, BBB] In essence, I need to ...

Utilizing web components from NPM packages in conjunction with Svelte

My current project involves the development of a simple Single Page App (SPA) using Svelte. I have successfully implemented a basic layout and styling, as well as an asynchronous web request triggered by a button click. Now, my next objective is to utiliz ...

Issue encountered when employing the spread operator on objects containing optional properties

To transform initial data into functional data, each with its own type, I need to address the optional names in the initial data. When converting to working data, I assign a default value of '__unknown__' for empty names. Check out this code sni ...

Issue encountered with AngularJS - module instantiation unsuccessful

I'm currently working my way through an Angular for .Net book, and I'm stuck on a basic example that involves creating an app with two controllers. However, I keep encountering this error message and I can't figure out why the module instant ...

Incorporating props into every page through getInitialProps in Next.js

I am trying to ensure that the same props are loaded on all pages I navigate to. My approach involves using _app.js as shown below: export default function MyApp({ Component, pageProps }) { return <Component {...pageProps} /> } MyApp.getInit ...

MongoDB nested aggregation query

I am facing challenges with aggregation in Mongo. The current JSON format is as follows: { "_id": { "$oid": "63074885ff3acbe0d63d7687" }, "iso_code": "AFG", "country": "Afghanista ...

Transforming a CSV file into JSON format using Gatsbyjs

I am currently exploring the possibilities of GatsbyJs and considering the utilization of the gatsby-transformer-csv plugin. You can find the documentation for this plugin here. I have got my hands on two CSV files exported from WordPress that I am eager ...

Set a delay for an AJAX request using jQuery

Is it possible to incorporate a setTimeout function into this ajax call? Here's the code snippet: jQuery.ajax({ type : "POST", url : dir+"all/money/myFile.php", data : "page="+data.replace(/\&/g, '^'), suc ...

Trail of crumbs leading to pages displayed in a div container

My website is designed with only one page where all other pages are loaded within a div. I am looking to implement breadcrumbs to keep track of the pages loaded inside the div. However, the solutions I have found online only work for pages loaded in the en ...

The attempt to define a 404 status for a document that cannot be found is unsuccessful in Mongoose

As I am diving into the world of MongoDB and mongoose, I have encountered an issue with setting a 404 status for my route handler. Below is the code snippet in question: app.get('/users/:id', async (req, res) => { const _id = req.params.id tr ...

Angular2 with Typescript is raising concerns over the absence of specific data types in

I am encountering an issue with the following code snippet: var headers = new Headers(); // headers.append('Content-Type', 'application/json'); headers.append('Content-Type ...

Error message in previous React-Redux project: nativeEvent.path is undefined

Currently, I am following a detailed guide to create a character sheet using React-Redux. https://www.youtube.com/watch?v=cPlejG83B1Y&list=PLJ-47dnNMd_jke6l27GmEiDk5XJGcr_HE&index=5 I have some basic knowledge of React from a tutorial I started y ...

The Veux Store is throwing an error message that says "Array is

When retrieving data from the Vuex Store, I start by checking if the array is present. Following that, my next step is to verify whether the noProducts object at index 0 exists. This validation process is important because the tweakwiseSortedProducts vari ...

What could be causing my HTML to not display properly after making changes to a text node?

Can a DOM text node be altered in such a way: node.nodeValue = "foo <strong> bar </strong>" so that it displays the HTML correctly? Appreciate any help. ...

Having trouble with my router in the express app - the .find and .findByID methods are not working properly. Also,

In my current setup with NextJS/MERN stack, I am using the server.js file in NextJS to import API routes and make API calls. The routes seem to be functioning properly as there is activity when calling them from Postman or the browser. However, it appears ...

Dealing with hidden elements poses a challenge for Selenium as it struggles to catch ElementNotVisibleException

Currently, I am working on creating user interface tests using selenium and I came across a method that is supposed to handle non-existing elements and hidden elements. The issue arises in the second catch block where the method consistently returns &apos ...

Tips for clearing out text in a <p> tag with javascript when it exceeds a specific length of 100 characters

Is there a way to automatically truncate text to (...) when it exceeds 100 characters inside a paragraph with the class .class? For instance, if I have a long paragraph like this: <div class='classname'> <p>Lorem ipsum dolor sit ame ...