The results of MongoDB aggregation queries consistently yield null values

Here is the initial query:

[{
            $match: {
                $and: [
                    {campaignID: "12345"},
                    {date: "2016-10-18"},
                    {hour: {$gte: 0}},
                    {hour: {$lte: 3}}
                ]
            }
        }
}]

The result of this query shows the following correct data:

[{"_id":"580638b1e4b01c1a027e82c1","campaignID":"12345","date":"2016-10-18",
"hour":0,"foo":10,"bar":10},
{"_id":"580638cbe4b01c1a027e82c2","campaignID":"12345","date":"2016-10-18",
"hour":1,"foo":11,"bar":11},
{"_id":"580638e5e4b01c1a027e82c3","campaignID":"12345","date":"2016-10-18",
"hour":2,"foo":12,"bar":12},
{"_id":"580638efe4b01c1a027e82c4","campaignID":"12345","date":"2016-10-18",
"hour":3,"foo":13,"bar":13}]

However, a different query was utilized:

[
        {
            $match: {
                $and: [
                    {campaignID: "12345"},
                    {date: "2016-10-18"},
                    {hour: {$gte: 0}},
                    {hour: {$lte: 3}}
                ]
            }
        },
        {
            $group: {
                _id: campaignID,
                foo:{$sum: "foo"},
                bar:{$sum: "bar"}
            }
        }
]

This second query produced incorrect data:

[{"_id":"sA48VsoQyuuEDVI5lr4loL31mqoCRT","foo":0,"bar":0}]

The desired total for both 'foo' and 'bar' should be 46 (10+11+12+13). What could be the issue here?

Answer №1

When referencing the value of a field, always use $ followed by the field name.

$group: {
    _id: "$campaignID",
    totalFoo: {$sum: "$foo"},
    totalBar: {$sum: "$bar"}
}

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

Determine the height of an image using JavaScript

How can I retrieve the height of an image in a JavaScript function? When I use the code: var image_height = $(image).height(); The value of image_height is 0, even though my image definitely has non-zero height. Is there a different method to accurately ...

Transmitting a JSON object through an HTTP POST request on an Android device

This is not a duplicate. The provided link is outdated as "http client" has been removed in api23. I am attempting to send a JSON object: {"emailId":"example@example.com","address":"Naya bans","city":"Noida","pincode":"201301","account_number":"911235463 ...

Combining res.render and redirect in Express.js for efficient rendering and redirection

I am currently developing a web application that involves setting up routes for user authentication. The Challenge: After a successful registration, I want to redirect the user to /login page while also including render options. However, when I use render ...

Is there a way to set up an automatic pop-up for this?

Experience this code function AutoPopup() { setTimeout(function () { document.getElementById('ac-wrapper').style.display = "block"; }, 5000); } #ac-wrapper { position: fixed; top: 0; left: 0; width: 100%; height: 100%; back ...

Executing NodeJS commands and Linux commands via Crontab scheduling

I have an EC2 AWS instance where various commands and scripts work perfectly, but do not execute within a crontab. The Crontab contains: 15 03 * * * pythonscript1.py 45 03 * * * pythonscript2.py 00 05 * * * gsjson 1z5OlqsyU5N2ze1JYHJssfe1LpKNvsr4j8TDGnvyu ...

Rubaxa-Sortable encountered an error while attempting to execute the 'matches' function on the 'Element': the selector '>*' is invalid

I have implemented Rubaxa Sortable JavaScript library in my Vue.js project. It works perfectly fine with <ul><li> elements, but when I try to use it with table rows, I encounter this error during drag-and-drop: Sortable.min.js:3 Uncaught DOMEx ...

Access the data from a table row when a checkbox is selected

Edit: I want to rephrase my inquiry I am attempting to retrieve the text inside the anchor tags within a <td> when a checkbox is checked. The issue is that the anchor tags do not have an id or class assigned to them. Below is a simplified version ...

What is the most strategic way to conceal this overlay element?

Currently, the website I'm developing features a series of navigation elements at the top such as "Products" and "Company." Upon hovering over the Products link, an overlay displays a list of products with clickable links. Positioned at the top of the ...

Access the hidden variable data of the parent page within a child iframe

I have a webpage with an embedded iframe that loads a PDF file after performing calculations. These calculations are done on the server side based on parameters stored in a hidden variable on the parent page. Here is the HTML markup of the parent page: ...

What are the best methods for profiling a Node.js application at a specific point in its execution?

I am currently facing performance issues with my Node application, which listens to a websocket data feed and communicates with another API. The CPU usage is normally stable at around 2-5%, but occasionally (approximately 3 times within 24 hours) the incom ...

Steps to create folders in dat.gui:

I'm attempting to incorporate folders for the lights in a three.js project that I borrowed from a three.js example. However, I am struggling to make it work. From what I gather, I should use f1=add.folder('light1') and then somehow add the p ...

Having trouble locating the correct path to access wins data in the JSON body?

I am currently in the process of developing an external adapter for my Chainlink node. I am making a POST API call from RapidAPI based on the name field and this is the body that is returned. https://i.sstatic.net/vGsVT.png I want to extract the "wins" da ...

add the closing </div> tag using jquery only

Having a slight issue here, it seems that jQuery is being overly clever. Within my HTML code, I am attempting to insert this string into a div container: </div><div class="something"> You'll notice that the closing tag comes first, foll ...

A guide on verifying discrepancies between selected record data in two tables

How can I validate if the data of selected records is mismatching in two tables? I need to select a few records in each table. Upon clicking the Match button, if the currencies are not the same, we need to display an alert saying "Data mismatched". I have ...

What is the simplest way to display an HTTP response in an alert window?

Struggling to display the JSON response using JavaScript's window.alert or alert. As a non-native JS coder, I apologize for my lack of experience. Here are a few attempts I've made based on online examples. My objective is to showcase the JSON r ...

Error: Unable to access the value property of a null object (React/JS/TS)

I created a function that dynamically determines the background color based on a specific value. const backgroundColorResolver = () => { allQuestions.map((aq) => { if (aq.averageAnswerValue <= 4) return "#EE7362"; if (a ...

What's the best way to fill checkboxes in EJS when editing a route?

As a beginner, I am working on my first project - a simple content/property listings app. I have created a form to collect user data and display it on a show form. The form includes sections for checkboxes and radio buttons, with the values being stored i ...

Display dynamic web content developed with JavaScript for public viewing

As a newcomer to the world of web development, I'm not sure if it's possible to achieve what I have in mind without using PHP. However, I'm willing to give it a shot. Currently, my webpage functions perfectly with JavaScript, HTML, and CSS, ...

How to incorporate Vue3 into Django using CDN without the need for a NodeJs bundler?

When working with Vue2, all I had to do was add the CDN and then Vue would be available in my JavaScript files. However, when trying to use Vue3, my JavaScript files are not detecting Vue. How can I troubleshoot this issue and start using Vue3 successful ...

Turning off the AngularJS dropdown multiselect functionality

I need a directive that can disable the dropdown multiselect in AngularJS. It's currently functioning properly when the checkbox is changed, but it's not working when the controller initializes. I want the dropdown to be disabled based on the val ...