Skip to content
Advertisement

Find objects created in last week in mongo

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

db.articles.find({
    timestamp:{
       '$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
},
votes:{
    up: [ObjectID],
    down: [ObjectID]
},
comments:[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.

Advertisement

Answer

Seems the solution should look like

db.articles.find({
    timestamp: {
        $gte: new Date(new Date() - 7 * 60 * 60 * 24 * 1000)
    }
});
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement