Get data from only one index between 2 dates MongoDB

Viewed 18

In the MongoDB, data is stored as such:

_id:61fba192f7a1d90a30b59531
date:1643880850452
testvalue:10
finalresult:0

My question now is how do I get only the value of finalresult in a between two dates back? For example between date:1643880850452 and date:1643880950452

1 Answers
db.collection.aggregate([
{
   '$match': {
        date: {'$lt': 1643880950452, '$gt': 1643880850452}
        }
},
{
    '$project': { finalresult: 1, _id: 0}
}])

You could first filter only the documents between the dates and then project the required field

Related