Combine mongoose $sum aggregations

I am facing a query that looks like the following:

{"$match": {$or: [{"to": system.mongoose.Types.ObjectId(userId)}, {"from": system.mongoose.Types.ObjectId(userId)}]}},
        {"$sort": {"createDate": -1}},
        {
            "$group": {
                "_id": "$conversationId",
                "from": {"$first": "$from"},
                "to": {"$first": "$to"},
                "content": {"$first": "$content"},
                "createDate": {"$first": "$createDate"},
                "unreaded": {"$sum": 1}
            }

        },

My goal is to calculate the total number of unreaded messages without altering the main $match.

Is there a way to achieve this with something similar to:

"unreaded": {"$sum": 1, {$match: {unreaded: true}}}

If you have any solution for this, please let me know.

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

Error in polling when integrating Coinpayments with Node.js

Everything is running smoothly with the code, but after a few seconds, an unexpected 'Polling error' message pops up in the console. I tried looking for solutions online, but unfortunately, I couldn't find anything helpful. If anyone has a ...

Error encountered while setting up dependencies on VPS due to SQLite problem

I am currently in the process of hosting a Discord bot developed in TypeScript on my VPS provided by galaxygate, running Ubuntu 20.04. Fortunately, Git was already installed on the server, so I cloned and pulled my project from GitHub. Next, I attempted t ...

Scan asynchronously and recursively through directories to list files using Node.js and Express.js

The challenge in this Expressjs route file lies in recursively retrieving all the JSON files within the ./data directory. While I am able to use console.log to display individual file paths, I am struggling to find a way to collect and send the complete l ...

When the disk space is insufficient, the createWriteStream function will not trigger an error event if the file is not completely written

One challenge I'm encountering involves using createWriteStream: Imagine I have a large 100mb file that I want to write to another file on the disk. The available space on the disk is only 50mb. Here's my code snippet: const fs = require(&a ...

How to include duration in a date field in MongoDB

Currently, I am working on honing my skills in mongodb and nodejs, but have encountered a hurdle along the way. Within my mongoDB database, there is a table named Schedule containing fields such as id, start, and end. My objective is to augment the start t ...

Unable to update values in Google Sheets using the node.js API

I have been working on a node.js project that involves extracting the location of a cell based on a person's name and date. While I am able to determine this information easily, I encounter difficulties when trying to update the cell using the .update ...

Setting up environment variables on Heroku: A step-by-step guide

After deploying my Node.js API on Heroku, the response I receive is incorrect. res.json({"user":user,"auth-token":token}); localhost:3000/auth/login { "user": { "email": "<a href="/cdn-cgi/l/email-protectio ...

Error: The value of _id is null and cannot be read

I've been encountering this error and have scoured numerous forums without finding a solution. Any assistance would be greatly appreciated, as I've been struggling with this problem for a long time! (node:2225) UnhandledPromiseRejectionWarning: ...

When incorporating a JS React component in TypeScript, an error may occur stating that the JSX element type 'MyComponent' is not a valid constructor function for JSX elements

Currently, I am dealing with a JavaScript legacy project that utilizes the React framework. Within this project, there are React components defined which I wish to reuse in a completely different TypeScript React project. The JavaScript React component is ...

Sending updates to the client as soon as a database field is modified

I am currently utilizing socket.io to transmit data from my database to the client. However, my current code sends data to the client every second, even when the data remains unchanged. Is there a way for me to only send data when a field in the database h ...

passport verification never succeeds

After setting up a straightforward login page with the help of Express framework, MongoDB, and Passport for authentication, I am encountering an issue where my authentication always ends up in the fail state. Can anyone point out where I might be going wro ...

The error message thrown by DynamoDB BatchGet is always 'The key element supplied does not align with the schema'

I've been struggling with the DynamoDB batchGet operation for quite some time now. Here's my table definition using serverless: Resources: MyTable: Type: AWS::DynamoDB::Table DeletionPolicy: Retain Properties: TableName: MyTab ...

Tips on directing users towards HTTP

We recently made changes to our website and moved our DNS routing to a new server. However, we are encountering an issue where sometimes when users type in 'example.com', they are redirected to HTTPS://example.com, which is not enabled at the mom ...

Getting request parameters within Model in Loopback can be done by accessing the `ctx`

common/models/event.json { "name": "Event", "mongodb": { "collection": "event" }, "base": "PersistedModel", "idInjection": true, "options": { "validateUpsert": true }, "http": { "path": "organizer/:organizer_id/events" }, "properties": {}, "va ...

One container, numerous ports, a variety of domains, ONE SSL certificate

Looking for advice on setting up SSL for my 5-6 node.js web apps running on different ports that are proxied to two domains via Apache. Would a multi-domain/wildcard SSL certificate work if I import it into all applications? I'm seeking guidance in t ...

Ways to transmit information from a React application to a Node server

As a Nodejs beginner, I am using the rtsp-relay library for live streaming. Currently, it is working in the frontend when the URL is included in the server proxy object like this: rtsp://.....@..../Stream/Channel/10. However, I want users to be able to inp ...

The Mongoose .lt method is unable to process the post value

I came across this interesting Mongoose function snippet: exports.readSign = function(req, res) { if (req.user.roles.indexOf('admin') === 1) { Timesheet.find() .where('projectId').equals(req.params.projectId) ...

Uploading CSV or text files in Node.js

I am looking to develop a function that can upload CSV or txt files into MongoDB using the MEAN stack. The function should work by allowing me to upload a file, then it will verify if it is in text/csv format before uploading it to MongoDB. I have attemp ...

Using NodeJS to initiate a file opening operation

Can someone please guide me on how to open a file using nodejs without using exec? Specifically, I need to run a nodejs file that will automatically open a specific file stored on my local hard drive. I want to ensure that this process is secure and reliab ...

Accessing view, model, or collection property in AJAX 'beforeSend' with Backbone.js

Currently in my node application I am utilizing Backbone.js to handle various ajax calls from views, models and collections. To keep track of these calls, I have created a custom property named "myName" in each view, model and collection, with a unique v ...