Using a Mongo query to transform integers or doubles into strings, separating the values with commas and adding a dollar sign at the

Looking for help with an aggregate mongo query that will output results in the "$236,535.73" format. Can anyone provide assistance with this query?

I have tried using the following query but it is truncating my decimal values: "totalDue" : { $concat: [ " $", {"$toString" : "$totalDue.value"} ] }

My MongoDB structure can be seen in the image below: enter image description here

Answer №1

Currently, there is no built-in operator for converting a Double to a comma-separated formatted String in MongoDB 4.2. Performing this conversion may result in a complex query and slow performance, especially with large datasets.

It would be more efficient to handle this conversion at the application layer instead. Since you have tagged the question with javascript, consider using Intl.NumberFormat

const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })
console.log(formatter.format(236535.73))
// output: "$236,535.73"

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

Issue specific to iOS devices: AJAX-loaded HTML containing <script> tags fails to execute

The purpose of the code below is to import innerStuff.html into the content-container div. Once that is done, a script within innerStuff.html named submit_entry() is used to submit the form in innerStuff.html. Current Situation: Upon clicking the button, ...

The basic Node.js API for greeting the world encountered a connection failure

I'm currently working on setting up a basic hello world route using nodejs and express. After running node index.js, I see listening on port 3000 in the console, but when I attempt to access http://localhost:3000/helloworld, it just keeps trying to co ...

Unable to dynamically validate the input field for the name using Angular.js

Can someone assist me with an issue I'm having? I'm encountering an error while trying to dynamically validate input fields using Angular.js. Error: TypeError: Cannot read property '$invalid' of undefined Let me explain the code t ...

Can the mapnik node module be used with newer versions of Node (such as 4.0.0)?

I'm currently running node version 0.12.0, but I'm considering upgrading to node version 4.0.0. However, I have concerns about whether mapnik is compatible with the newer version of Node. The documentation only mentions support for Node v0.10.x o ...

When trying to reach the state link in a different component, it leads to a typerror indicating that the state is undefined

I'm trying to pass state down using the <Link> component to the NextPage component like this: <Link to={{pathname: `/job-run-id/${item.job_run_id}`, state: { prevPath: location.pathname }}} > {item.job_run_id} </Link> The NextPage c ...

Issue with PHP Upload: Index is undefined

Having Trouble with PHP Upload: Encountered an issue: Undefined index: file in Error on line: $count = count($_FILES['file']['name']); Tried numerous codes to fix this error, but none have worked so far PHP Code: <?php $count ...

How to transform a JSON object into a formatted string using Vue?

I have a dynamically set JSON object that needs to be manipulated. The method onDragStop is used to set the columns: data() { return { columns: [], } }, methods: { onDragStop: function (index) { this.$set(this.columns, index, { xAxis: ...

Next.js app experiencing issues with Chakra UI not transitioning to dark mode

After attempting to incorporate Chakra UI into my Next.js application, I carefully followed every step outlined in their documentation: Despite setting the initialColorMode to "dark" for the ColorModeScript prop, it seems that the dark mode is not being a ...

Incorporate additional text to the downloads hyperlink using jquery

Is it possible to achieve this using jQuery since I am unable to directly modify the HTML markup? I am looking to append download.php?file= to a URL without replacing the entire href attribute. Here's an example of what I'm trying to achieve: & ...

Problems with form functionality in React component using Material UI

Trying to set up a signup form for a web-app and encountering some issues. Using hooks in a function to render the signup page, which is routed from the login page. Currently, everything works fine when returning HTML directly from the function (signup), ...

What is the best way to manage various versions of JS libraries across different branches?

As a novice developer, I dabble in creating applications for personal use. My go-to tools are the Quasar framework for the front end and Python for the back end. I maintain a git repository where the master branch houses my "production code," but now I am ...

The header row in HTML tables sometimes vanishes unexpectedly after sorting the table

Upon filtering the table, I noticed that the header disappears sporadically. The issue is that the table header row should remain in place regardless of whether or not the characters used for filtering are present in the table rows. In Example 1: When fil ...

How to mute a particular warning in development mode with Next.js

Currently in the process of transitioning a CRA app to Next.js in order to enhance SEO. During development, I encountered the following warning: Warning: 'NaN' is an invalid value for the 'left' css style property. I am aware of the s ...

The object returns true when the specified condition matches the key within the object

I need assistance with a specific object query. I am looking to execute the filter function in order to retrieve a list of keys from an object where the condition is true, as shown below: myObject = { key1: { name:"key1", select:true }, ...

showing sections that collapse next to each other

I am currently designing a portfolio website using HTML, CSS, and vanilla JavaScript. I have implemented collapsing sections that expand when clicked on. However, the buttons for these sections are stacked vertically and I want to place them side by side. ...

What is the best way to insert a new row into a table upon clicking a button with Javascript?

Hi everyone, I'm facing an issue with my code. Whenever I click on "Add Product", I want a new row with the same fields to be added. However, it's not working as expected when I run the code. Below is the HTML: <table class="table" id="conci ...

Shadows on menu buttons transform when clicked using React and CSS

I'm currently working on customizing the styling of a menu using CSS in a project that involves the use of "react-horizontal-scrolling-menu". While I've been successful in styling the menu items with .menu-item:hover & .menu-item:active, I am ...

Trouble persists in saving local images from Multer array in both Express and React

I am having trouble saving files locally in my MERN app. No matter what I try, nothing seems to work. My goal is to upload an array of multiple images. Below is the code I have: collection.js const mongoose = require("mongoose"); let collectionSchema ...

Complete a checkbox entry form and showcase it within a "division" on the current page

Hello everyone, I need some help. I have a form that I want to send to a specific div on the same page. This is what I am aiming for. I have a floating div named "basket" in the top right corner of my page where I want the form data to be displayed after s ...

What is the best way to use async await to listen for an event in a child component with Vue-router?

To prevent users from unintentionally leaving an unsaved form, I am looking to implement a custom modal alert system with vue-router. Although the use of the standard browser alert box is an option, beforeRouteLeave (to, from, next) { const answer = wi ...