Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Date casting in geoNear queries #5765

Closed
pjo336 opened this issue Oct 28, 2017 · 4 comments
Closed

Incorrect Date casting in geoNear queries #5765

pjo336 opened this issue Oct 28, 2017 · 4 comments
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@pjo336
Copy link
Contributor

pjo336 commented Oct 28, 2017

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?

@vkarpov15 vkarpov15 added this to the 4.12.6 milestone Oct 30, 2017
@vkarpov15 vkarpov15 added the bug? label Oct 30, 2017
@vkarpov15
Copy link
Collaborator

Will investigate ASAP, thanks for reporting 👍

@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Oct 30, 2017
@vkarpov15 vkarpov15 modified the milestones: 4.12.6, 4.13.1 Nov 1, 2017
@sobafuchs
Copy link
Contributor

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()`
  });
}

@sobafuchs sobafuchs added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed bug? needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Nov 3, 2017
vkarpov15 added a commit that referenced this issue Nov 5, 2017
@vkarpov15
Copy link
Collaborator

Looks like we did not cast the query option at all 🤕 🤦‍♂️ . Fix will be in for 4.13.1

vkarpov15 added a commit that referenced this issue Nov 5, 2017
@pjo336
Copy link
Contributor Author

pjo336 commented Nov 5, 2017

👍 👍 👍 I tried and failed to add this myself :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants