learningtech
learningtech

Reputation: 33747

MongoDB + MeteorJS unable to filter by date?

I'm new to meteorjs and mongodb. I'm having trouble finding a collection of results based on date. First I used the following meteorjs code to confirm there are records.

var application = Applications.findOne({_id:'Y3xCNck6JhABGj9e7'});
var a1 = Applications.find({owner:application.owner}).map(function(app){return {owner:app.owner,createdAt:app.createdAt}});
console.log(application.createdAt); // gives Sun Dec 27 2015 09:19:17 GMT-0500 (Eastern Standard Time)
console.log(a1); 
/*
This gives plenty of results.  An example is
createdAt: Sun Dec 27 2015 09:16:10 GMT-0500 (Eastern Standard Time)
owner: "s4xcBwWAqSktoQuLA"

Note that the createdAt date for this particular record is before the application.createdAt, which will become relevant in the next set of queries.
*/

Now what I don't understand is why each of these statements give zero results:

var application = Applications.findOne({_id:'Y3xCNck6JhABGj9e7'});
var a2a = Applications.find({createAt:{$lt:application.createdAt}}).map(function(app){return {owner:app.owner,createdAt:app.createdAt}});
var a2b = Applications.find({createAt:{$lt: new Date(application.createdAt)}}).map(function(app){return {owner:app.owner,createdAt:app.createdAt}});
var a2c = Applications.find({createAt:{$lte:application.createdAt}}).map(function(app){return {owner:app.owner,createdAt:app.createdAt}});
var a2d = Applications.find({createAt:{$lte: new Date(application.createdAt)}}).map(function(app){return {owner:app.owner,createdAt:app.createdAt}});

What did I do wrong? In the a1 example, I'm sure there should be records with createdAt being less than the application.createdAt.

additional notes

When I first saved the application, I saved the date as follows:

  saveApplication(formVals) {
    if (! Meteor.userId()) {
      throw new Meteor.Error("not-authorized");
    }
    formVals['appStatus'] = 1;
    formVals['createdAt'] = new Date();
    formVals['owner'] = Meteor.userId();
    formVals['username'] = Meteor.user().username;
    Applications.insert(formVals);
  },

Upvotes: 0

Views: 57

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20256

Typos:

var a2a = Applications.find({ createAt: { $lt: application.createdAt }})...

should be

var a2a = Applications.find({ createdAt: { $lt: application.createdAt }})...

Upvotes: 2

Related Questions