0

I have the following objects in a MEC_CALENDAR_DATES collection. My aim is to query the documents in this collection from a Meteor project greater than or equal to a start date property Date:

{
    "_id" : "vT92uJzbiDheH4FYD",
    "key" : "MEC_CALENDAR_DATES",
    "Title" : "BBS",
    "Date" : ISODate("2017-11-21T00:00:00.000Z")
}

/* 2 */
{
    "_id" : "seGmNdwfQxTJnTxJz",
    "key" : "MEC_CALENDAR_DATES",
    "Title" : "AAS",
    "Date" : ISODate("2017-11-15T00:00:00.000Z")
}

What I've tried in RoboMongo is the following query which works and returns the objects greater than the supplied date for that collection:

db.getCollection('app_configuration').find({"key":"MEC_CALENDAR_DATES", "Date": {$gte: ISODate("2017-10-03T09:35:14.561Z")}}).sort({Date: -1})

But when I try to translate that to Meteor using the api zero results are returned:

const appConfigCollection = AppConfiguration.find({
  key: "MEC_CALENDAR_DATES",
  Date: {$gte:ISODate("2017-08-03T10:26:13.151Z")},
},
  {
    sort: {Date: -1},
});

console.log("appConfigCollection " + appConfigCollection);

Question:

How can you query documents $gte using Meteor api on a collection?

Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • By using `new Date()` instead of `ISODate()` like every other JavaScript program. And stated so in many answers here already. – Neil Lunn Aug 10 '17 at 10:28
  • @NeilLunn ok so you mean instead of `$gte:ISODate("2017-08-03T10:26:13.151Z")` use `$gte:new Date("2017-08-03T10:26:13.151Z")` – Brian Var Aug 10 '17 at 10:30
  • Yep. Same goes for the shell as well. `ISODate()` is really just a "placeholder" to identify a class for the BSON Date type. It's not a special function and really just wraps `Date` in it's implementation in the shell. `Date` is the correct type for JavaScript. Just as there is a general type for just about every other language. – Neil Lunn Aug 10 '17 at 10:31
  • hmm ok in my code I'm using the var oldestDate, getting the value using moment and converting to iso string. So maybe I should be converting to a date instead of isostring at this point? `const oldestDate = moment.utc().subtract(7, 'days').toISOString();` – Brian Var Aug 10 '17 at 10:33
  • `moment.utc().subtract(7, 'days').toDate()`. Already told you it's a `Date` object. Not an `ISODate` and not a `"string"`. – Neil Lunn Aug 10 '17 at 10:36
  • yeah makes sense that what I was getting at in my previous comment. – Brian Var Aug 10 '17 at 10:38
  • @BrianJ I suggest you try using the [Meteor Shell](http://docs.meteor.com/commandline.html#meteorshell) rather than the RoboMongo/Meteor Mongo, as it more closely replicates the environment your server side meteor code runs in. You can run queries against MongoDB, though you will need to import your Collection definitions first. e.g. `import { AppConfiguration } from '/appconfiguration.js'; AppConfiguration.find( ... ).fetch()` – JeremyK Aug 10 '17 at 10:46

0 Answers0