Mongo: 3.4.9 Node: 8.1.2 Mongoose: 4.12.4
In general when you run a query using a date, such as a count, you can write something like
return Foo.count({ imADate: { '$gte': new Date() } })
or using Moment JS:
return Foo.count({ imADate: { '$gte': moment().toISOString() } })
and a few other variations.
Debug output shows something to the effect of:
Mongoose: foos.count({ imADate: { '$gte': new Date("Sat, 28 Oct 2017 03:49:28 GMT") } }, {})
All is well and good.
However in the query field on a geoNear query:
return Foo.geoNear({
type: 'Point',
coordinates: [lng, lat],
}, {
spherical: true,
maxDistance: 100000,
num: 10,
query: { imADate: { '$gte' : moment().toISOString() } }
})
No such casting seems to occur. The result outputs:
Mongoose: foos.geoNear({
type: 'Point', coordinates: [ -118.24368, 34.05223 ] }, { spherical: true, maxDistance: 100000, num: 10, query: { imADate: { '$gte': '2017-10-28T03:53:23.395Z' } } })
The date is not wrapped in a new Date(
and therefore doesn't work. Is there improper use on my part or is this a bug?
Will investigate ASAP, thanks for reporting :+1:
yup it does look like in the geoNear queries it does it differently:
const mongoose = require('mongoose');
const co = require('co');
mongoose.Promise = global.Promise;
const GITHUB_ISSUE = `gh-5765`;
mongoose.set('debug', true);
exec()
.then(() => {
console.log('successfully ran program');
process.exit(0);
})
.catch(error => {
console.error(`Error: ${error}\n${error.stack}`);
});
function exec() {
return co(function* () {
yield mongoose.connect(`mongodb://localhost:27017/${GITHUB_ISSUE}`, { useMongoClient: true });
const schema = new mongoose.Schema({
coords: {
type: [Number],
index: '2dsphere'
},
imADate: Date
});
const Model = mongoose.model('Model', schema);
yield Model.ensureIndexes();
yield Model.geoNear({
type: 'Point',
coordinates: [-118.24368, 34.05223],
}, {
spherical: true,
maxDistance: 100000,
num: 10,
query: { imADate: { '$gte': new Date().toISOString() } } // queries date with '2017-11-03T02:55:40.644Z'
});
yield Model.find({ imADate: new Date().toISOString() }); // correctly puts `new Date()`
});
}
Looks like we did not cast the query
option at all :face_with_head_bandage: :man_facepalming: . Fix will be in for 4.13.1
馃憤 馃憤 馃憤 I tried and failed to add this myself :)