Search code examples
mongodbmongodb-queryunix-timestampnosql

Query date based using milliseconds time on MongoDB


I need to search all records that match a date condition. On MongoDB I've a bunch of data like this:

{
    "_id" : "9ed3b937-0f43-4613-bd58-cb739a8c5bf6",
    "userModels" : {
        "5080" : {
            "generated_date_timestamp" : NumberLong(1413382499442),
            "model_id" : 5080,
        },
    }
    "values" : {}
}

I'm not able to convert that NumberLong in something that can be used by this query:

db.anonProfile.find({ 
   "userModels.5080.generated_date_timestamp" : { "$gte" : ISODate("2013-10-01T00:00:00.000Z") }
});

"_id" has been saved as String so I cannot use for a ObjectID search. [btw, is it possible to do?]

Any clue?

Tnx, Andrea


Solution

  • You can query by

    db.anonProfile.find({ 
       "userModels.5080.generated_date_timestamp" : { "$gte" : ISODate("2013-10-01T00:00:00.000Z").getTime() }
    });