Get the maximum of difference of two fields in mongodb

I'm newbie to mongoDB. Here I face with an error while I want to get the maximum value of the difference of two fields.

I'm newbie to mongoDB. Here I face with an error while I want to get the maximum value of the difference of two fields.

Here is the structure of data saved in database:

{
    "_id" : ObjectId("52b3833bd3e98582d2bfb628"),
    "author" : {
        "name" : "Graydon Hoare",
        "email" : "[email protected]"
    },
    "title" : "Why Rust ditched pure functions",
    "body" : "sth",
    "url" : "http://thread.gmane.org/gmane.comp.lang.rust.devel/3674/focus=3855",
    "date" : ISODate("2013-04-30T13:23:00.000Z"),
    "starred" : 105,
    "ratings" : [ 
        3, 
        5, 
        3, 
        2, 
        4, 
        1, 
        3, 
        3, 
        3, 
        2, 
        3
    ],
    "comments" : [ 
        {
            "user" : "tr0lltherapy",
            "upVotes" : 18,
            "downVotes" : 2,
            "text" : "sth",
            "replies" : [ 
                {
                    "user" : "thedeemon",
                    "upVotes" : 10,
                    "downVotes" : 0,
                    "text" : "sth"
                }, 
                {
                    "user" : "mcandre",
                    "upVotes" : 0,
                    "downVotes" : 5,
                    "text" : "sth"
                }, 
                {
                    "user" : "lacosaes0",
                    "upVotes" : 30,
                    "downVotes" : 6,
                    "text" : "Particular emphasis on memory safety."
                }
            ]
        }, 
        {
            "user" : "hypster",
            "upVotes" : 30,
            "downVotes" : 2,
            "text" : "tl;dr everybody was type-fu fighting",
            "replies" : [ 
                {
                    "user" : "homoiconic",
                    "upVotes" : 15,
                    "downVotes" : 0,
                    "text" : "Here comes the Big Boss, Hu! Simon Peyton-Jones."
                }
            ]
        }
    ],
    "tags" : [ 
        "Rust", 
        "Computer", 
        "Programming"
    ],
    "draft" : true,
    "published" : true
}

What I want is to get the value of maximum of the subtract of upVotes and downVotes in repliesand comments.

db.getCollection('links').aggregate([
    {$project: {
        _id: "$author",
        maxVote: $max: {
                $subtract: ["$comments.upVotes", "$comments.downVotes"]
                }
            } 
     }
])

I don't know how to fix it!

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB - Part 3/3

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB

What you’ll learn

  • Work with MongoDB with Clarity and Confidence
  • Use 4 tools MongoCHEF, NOSQL Manager, RoboMongo, MongoBooster easily
  • Do Regex, GridFS , Replication , Sharding, Full text search
  • Basic and Advanced CRUD operations using MongoDB
  • Import and Export data from MongoDB
  • Work MapReduce, Embedded Documents,Save&Insert , indexing, capped collections, TTL
  • Bonus section * Use java,C#,PHP,Nodejs to access MongoDB features like CRUD, GridFS
  • Bonus Section * A 50 minutes MongoDB key feature exercises
  • 100+ Quizzes 40+ Activities

Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

MongoDB - The Complete Developer’s Guide

Learn MongoDB : Leading NoSQL Database from scratch

Learn NoSQL Databases - Complete MongoDB Bootcamp 2019

Build a CRUD Operation using PHP & MongoBD

MongoDB with Python Crash Course - Tutorial for Beginners

Learn NoSQL Databases from Scratch - Complete MongoDB Bootcamp 2019

AngularJS tutorial for beginners with NodeJS, ExpressJS and MongoDB

MEAN Stack Tutorial MongoDB, ExpressJS, AngularJS and NodeJS

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB - Part 1/3

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB

What you’ll learn

  • Work with MongoDB with Clarity and Confidence
  • Use 4 tools MongoCHEF, NOSQL Manager, RoboMongo, MongoBooster easily
  • Do Regex, GridFS , Replication , Sharding, Full text search
  • Basic and Advanced CRUD operations using MongoDB
  • Import and Export data from MongoDB
  • Work MapReduce, Embedded Documents,Save&Insert , indexing, capped collections, TTL
  • Bonus section * Use java,C#,PHP,Nodejs to access MongoDB features like CRUD, GridFS
  • Bonus Section * A 50 minutes MongoDB key feature exercises
  • 100+ Quizzes 40+ Activities


Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

MongoDB - The Complete Developer’s Guide

Building A REST API With MongoDB, Mongoose, And Node.js

Node.js, ExpressJs, MongoDB and Vue.js (MEVN Stack) Application Tutorial

MEAN Stack Tutorial MongoDB, ExpressJS, AngularJS and NodeJS

MongoDB with Python Crash Course - Tutorial for Beginners

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB - Part 2/3

Learn MongoDB - MongoDB Tutorial for Beginners - Getting Started with MongoDB


What you’ll learn

  • Work with MongoDB with Clarity and Confidence
  • Use 4 tools MongoCHEF, NOSQL Manager, RoboMongo, MongoBooster easily
  • Do Regex, GridFS , Replication , Sharding, Full text search
  • Basic and Advanced CRUD operations using MongoDB
  • Import and Export data from MongoDB
  • Work MapReduce, Embedded Documents,Save&Insert , indexing, capped collections, TTL
  • Bonus section * Use java,C#,PHP,Nodejs to access MongoDB features like CRUD, GridFS
  • Bonus Section * A 50 minutes MongoDB key feature exercises
  • 100+ Quizzes 40+ Activities

Learn More

MongoDB - The Complete Developer’s Guide

The Complete Developers Guide to MongoDB

MongoDB - The Complete Developer’s Guide

Learn MongoDB : Leading NoSQL Database from scratch

Learn NoSQL Databases - Complete MongoDB Bootcamp 2019

Why We Moved From NoSQL MongoDB to PostgreSQL?

How to build GraphQL APIs with Kotlin, Spring Boot, and MongoDB?

Build a CRUD Operation using PHP & MongoBD

MongoDB with Python Crash Course - Tutorial for Beginners

Learn NoSQL Databases from Scratch - Complete MongoDB Bootcamp 2019