Find objects created in last week in mongo

Tags: , ,

I have a collection which has a field called timestamp containing date object. I have this query:

       '$lte':new Date(),
       '$gte': //Something to get the last week's date

Also if it is possible, Can I sort these returned documents by length of an array in this document. Here is the schema:

section: String,
title: String,
abstract: String,
url: String,
image: {
    url: String,
    caption: String
    up: [ObjectID],
    down: [ObjectID]
timestamp: Date

I want to sort the returned objects by size of difference of votes.up and votes.down. Right now I am sorting the returned objects in Javascript where this actually returns the data.


Seems the solution should look like

    timestamp: {
        $gte: new Date(new Date() - 7 * 60 * 60 * 24 * 1000)

Source: stackoverflow