Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date comparison with Mongoose

I'm trying to find a user with a user that has a location that is newer or older than a certain date.

Schema

var userSchema = new Schema({
    firstName: String,
    lastName: String,
    email: String,
    facebookId: String,
    googleId: String,
    token: {type: String, unique: true, required: true},
    friends: [userSchema],
    location: {
        type: {
            timestamp: Date,
            accuracy: Number,
            coordinates: {
                lat: Number,
                lng: Number
            }
        },
        default: null
    }
}, {

Query

userSchema.statics.findRecentUpdates = function (ids, minutesSinceLast, callback) {
    var recentDate = new Date().removeMinutes(minutesSinceLast);
    this.find(
        {'location.timestamp': {$lt: recentDate}},
        callback
    );
}

Data

db.users.find();
{ "__v" : 1, "_id" : ObjectId("53cecb60fe7d090000030328"), "email" : "[email protected]", "facebookId" : "9138129367", "firstName" : "Cornel", "friends" : [  ObjectId("53cecb4dfe7d090000030327") ], "lastName" : "GD", "location" : { "coordinates" : { "lat" : 18.040808, "lng" : 59.330557 }, "timestamp" : 1406061408964, "accuracy" : 1 }, "token" : "988bb52b-cc0c-492d-8c75-f1f7d882157d" }
{ "__v" : 20, "_id" : ObjectId("53cf668eff9ef40000f76ce5"), "email" : "[email protected]", "facebookId" : "5762365752", "firstName" : "Yuan", "friends" : [ ], "lastName" : "vh", "location" : { "accuracy" : 3, "coordinates" : { "lat" : 59.356862, "lng" : 17.996661 }, "timestamp" : 1406101134199 }, "token" : "51278cb7-9e55-4cc5-b635-7cd289b2fa6a" }
{ "__v" : 21, "_id" : ObjectId("53cecb4dfe7d090000030327"), "email" : "[email protected]", "facebookId" : "7237853051", "firstName" : "M", "friends" : [  ObjectId("53cecb60fe7d090000030328") ], "lastName" : "H", "location" : { "accuracy" : 0, "coordinates" : { "lng" : 18.0246476, "lat" : 59.3827026 }, "timestamp" : 1413838822856 }, "token" : "5d870480-05c0-4a83-ae44-cfe39692d308" }

No matter what I set the date to I always get an empty result. Trying with 'location.accuracy' and setting less or greater than an that works.

like image 686
softarn Avatar asked Oct 20 '14 21:10

softarn


People also ask

What is the type of date in mongoose?

Date , or simply Mongoose Date , is a Date SchemaType . In the example above, String , Number , and Date are all SchemaTypes . This means that they configure the fields of any document stored in our database. The example above shows how Date is stored in a MongoDB database.

How do you compare date objects?

The date object allows us to perform comparisons using the > , < , = , or >= comparison operators, but not the equality comparison operators like == , != , === , and !== (unless we attach date methods to the date Object).

Can you compare JavaScript dates?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.

Is mongoose faster than MongoDB?

Mongoose, a neat ODM library for MongoDB used in Node. js projects, has plenty of useful features that make developers' lives easier. It manages relationships between data, has schema validation, and overall allows coding 3-5 times faster.


2 Answers

The error is in how I stored the dates. As we can see from the data above they are stored as milliseconds 1406101134199 and not as the ISODate format ISODate("2014-10-20T21:50:23.196Z"). This was due to the line Date.now() which does not return the current date object but milliseconds instead. Using new Date() fixed the error.

like image 122
softarn Avatar answered Oct 11 '22 16:10

softarn


Need to put removeMinutes after assigning the new Date();

Query:

userSchema.statics.findRecentUpdates = function (ids, minutesSinceLast, callback) {
    var recentDate = new Date();
    recentDate.removeMinutes(minutesSinceLast); // It will remove the minutes
    this.find(
        {'location.timestamp': {$lt: recentDate}},
        callback
    );
}
like image 29
Vishnu Mishra Avatar answered Oct 11 '22 16:10

Vishnu Mishra