Q:

Calculating the average for last 200 days in MongoDB aggregation and NodeJS

I have a script which is calculating the average Volume data for every collection in my MongoDB. But since I have more that a 1000 days in record I wish to calculate the Average Volume only for last 200 days.
Currently my script taking the whole data which is in collection (1000days+).
Goal: How I can setup a range of last 200 days to calculate the average Volume for them. Also pretty important thing that every next day I still want to use only last 200 days.
Average aggregation Framework
const saveToDatebase = async(symbol, BTCdata) => {
    try {
        const url = 'mongodb://username:passwprd@ipaddress/port?retryWrites=true&w=majority';
        let dateTime = getDateTime();
        let db = await MongoClient.connect(url, { useUnifiedTopology: true });
        const dbo = db.db('Crypto');
        const myobj = Object.assign({ Name: symbol, Date: dateTime }, BTCdata[0]);
        await dbo.collection(symbol).insertOne(myobj);
        const average = await dbo.collection(symbol).aggregate([{
            '$group': {
            _id:null,
                'Volume': {
                    '$avg': '$Volume'
                }
            }
        }]).toArray();
        console.log('1 document inserted');
        console.log(average);
        db.close();
    } catch (e) {
        console.error(e)
    }
};

EDIT1
After editing the code like this >>
const average = await dbo.collection(symbol).aggregate([{
    $match: {
        $gte: ["$dateTime", { $subtract: ["$$NOW", 200 * 60 * 60 * 24 * 1000] }]
    },
    "$group": {
        _id: null,
        "Volume": {
            "$avg": "$Volume"
        }
    }
}]).toArray();

I receive this error > MongoError: A pipeline stage specification object must contain exactly one field.
EDIT2
Code After having code like below I receive this error - MongoError: unknown top level operator: $gte
const average = await dbo.collection(symbol).aggregate([{
                $match: { $gte: ["$dateTime", { $subtract: ["$$NOW", 200 * 60 * 60 * 24 * 1000] }] }
            },
            {
                "$group": {
                    _id: null,
                    "Volume": {
                        "$avg": "$Volume"
                    }
                }
            }
        ])

Here is how document looks like in MongoDB.

EDIT3
Here is how my code looks like regarding the last improvement. Code is working the only problem It didn't return me average. In my terminal I receive an empty array [] but I don't receive any error. I'm thinking that the problem is here - "$dateTime" I think the query for it is wrong. Cause I'm also trying to make it in MongoDB Compass. Doesn't work as well.
Code Here
  const average = await dbo.collection(symbol).aggregate([{
            $match: {
                $expr: {
                    $gte: [
                        "$dateTime",
                        {
                            $subtract: [
                                "$$NOW",
                                200 * 60 * 60 * 24 * 1000
                            ]
                        }
                    ]
                }
            }
        },
        {
            "$group": {
                _id: null,
                "Volume": {
                    "$avg": "$Volume"
                }
            }
        }
    ]).toArray();

EDIT4
The only problem now that I receive an empty array > [] I receive an empty array cause the Date is saving as string to MongoDB but to make aggregation framework work I need to store it as object.
In code below there is my way of creating Date function it saves it in MongoDB as string how I can change it to save it as object
const getDateTime = () => {
    let today = new Date();
    let date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
    let time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    return date + ' ' + time;
};

const saveToDatebase = async(symbol, BTCdata) => {
    try {
        const url = 'mongodb://username:password@ipadress/port?dbname?retryWrites=true&w=majority';
        let dateTime = getDateTime();
        let db = await MongoClient.connect(url, { useUnifiedTopology: true });
        const dbo = db.db('Crypto');
        const myobj = Object.assign({ Name: symbol, Date: dateTime }, BTCdata[0]);
        await dbo.collection(symbol).insertOne(myobj);
        const average = await dbo.collection(symbol).aggregate([{
                '$match': {
                    'dateTime': { '$gte': new Date((new Date().getTime() - (7 * 24 * 60 * 60 * 1000))) }
                },
            },
            {
                '$group': {
                    _id: null,
                    'Volume': { '$avg': '$Volume' }
                },
            }
        ]).toArray();
        console.log('1 document inserted');
        console.log(average);
        db.close();
    } catch (e) {
        console.error(e)
    }
};

Document from MongoDB as text:
    {
    "_id": {
        "$oid": "5f158c9d80d84408f6c38d8b"
    },
    "Name": "ADABTC",
    "Date": "2020-7-20 13:22:53",
    "Open": 0.0000133,
    "High": 0.0000133,
    "Low": 0.00001325,
    "Close": 0.00001326,
    "Volume": 1189734,
    "Timespan": 30
}

A:

You have to filter documents before you group them. Should be like this:
db.collection.aggregate([
   {
      $addFields: {
         DateObj: {
            $regexFindAll: { input: "$Date", regex: "\\d+" }
         }
      }
   },
   {
      $set: {
         DateObj: {
            $dateFromParts: {
               year: { $toInt: { $arrayElemAt: ["$DateObj.match", 0] } },
               month: { $toInt: { $arrayElemAt: ["$DateObj.match", 1] } },
               day: { $toInt: { $arrayElemAt: ["$DateObj.match", 2] } },
               hour: { $toInt: { $arrayElemAt: ["$DateObj.match", 3] } },
               minute: { $toInt: { $arrayElemAt: ["$DateObj.match", 4] } },
               second: { $toInt: { $arrayElemAt: ["$DateObj.match", 5] } },
               timezone: "Europe/Zurich"
            }
         }
      }
   },
   {
      $match: {
         $expr: {
            $gte: ["$DateObj", { $subtract: ["$$NOW", 200 * 60 * 60 * 24 * 1000] }]
         }
      }
   },
   {
      "$group": {
         _id: null,
         "Volume": {
            "$avg": "$Volume"
         }
      }
   }
])

