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

Unable to perform geospatial queries #4014

Closed
angelo0000 opened this issue Mar 22, 2016 · 17 comments
Closed

Unable to perform geospatial queries #4014

angelo0000 opened this issue Mar 22, 2016 · 17 comments
Milestone

Comments

@angelo0000
Copy link

When trying to issue a geospatial query using 'near' you get back an error "Can't use $near". I have also tried with geoNear and nearSphere as well.

Example:

var Schema = new mongoose.Schema({
    placeName: String,
    geo: {
        type: { type: String, enum: "Point", default: "Point" },
        coordinates: { type: [ Number ],   default: [ 0,0 ] }    
    }
});

somemodel.where('geo').near({ center: [50, 50] }).exec(function(err, locs){
  console.log(locs);
})

The above will return: [Error: Can't use $near]

I have also tried the following:

model.find({
    geo: {
        $near: {
            $geometry: {
                type: "Point",
                coordinates: [50, 50]
            },
            $maxDistance: maxDistance
        }
    }
}, function(err, locs) {
    console.log(locs);
});

It will also result in the same error: [Error: Can't use $near]

@vkarpov15
Copy link
Collaborator

Can you execute this same query in the mongo shell? This seems like an error with how your query is structured rather than with mongoose

@vkarpov15 vkarpov15 added this to the 4.4.11 milestone Mar 25, 2016
@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Mar 25, 2016
@angelo0000
Copy link
Author

Yes I was able to execute the same query in monghub actually.

@vkarpov15
Copy link
Collaborator

Can you clarify which version of mongoose you're using, and show me the output from running your query with mongoose debug mode on? require('mongoose').set('debug', true)

@Iliyass
Copy link

Iliyass commented Apr 1, 2016

@vkarpov15 I have the same problem, I have executed the same query in Mongo shell and it works, but when I try it in mongoose, it doesn't works and it throws message: Can't use $near
this is the attribute schema:
const Geo = new Schema({ lng: { type: Number, required: true }, lat: { type: Number, required: true } }
geo: { type: Geo, required: true }
I have setup mongoose to debug mode, but there is no info shown for the query executed
Version: Mongoose 4.4.6

@vkarpov15
Copy link
Collaborator

@angelo0000 @Iliyass the below script executes without error for me:

'use strict';

const assert = require('assert');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/gh4014');
mongoose.set('debug', true);

var schema = new mongoose.Schema({
  placeName: String,
  geo: {
    type: {
      type: String,
      enum: 'Point',
      default: 'Point'
    },    
    coordinates: {
      type: [Number],
      default: [0, 0]
    }     
  }     
});   

var MyModel = mongoose.model('gh4014', schema);

MyModel.
  where('geo').near({ center: [50, 50] }).
  exec(function(error) {
    assert.ifError(error);
    console.log('done');
    process.exit(0);
  });

Can you clarify which version of mongodb you're using, and see if you can modify the above script to reproduce your issue?

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Apr 2, 2016
@vkarpov15 vkarpov15 modified the milestones: 4.4.12, 4.4.11 Apr 2, 2016
@Iliyass
Copy link

Iliyass commented Apr 3, 2016

@vkarpov15
Actually my schema is different than yours, here is mine:

    ...,
    geo: {
      type: new Schema({
                    lng: { type: Number, required: true },
                    lat: { type: Number, required: true }
               }, { _id : false }),
      required: true 
    }

can I still make a geospatial based on this schema, because it works in MongoDb shell ?

@vkarpov15
Copy link
Collaborator

What does the result of the geospatial query look like in the shell?

@MaarekElets
Copy link

Are either @Iliyass or @angelo0000 using something like this to ensure that the geo type is always set to "Point"?

Schema.pre('save', function(next) {
            this.geo.type = 'Point';
            next();
        });

I removed this from my schema and the Can't use $near/$nearSphere error went away.

Also, I'm not seeing any 2d or 2dsphere indexes in any of the examples. Aren't these required for $near or $nearSphere? Maybe they are being left out as extraneous info but just wanted to make sure I wasn't misunderstanding how this works as well.

@Iliyass
Copy link

Iliyass commented Apr 5, 2016

@vkarpov15 it looks normal, like any find, if I didn't set the $maxDistance it would look like it's sorted from the closest to the farest.
MongoDB Shell Query:

 db.users.find({ geo: { $near: [ 37.33259551999998, -122.03031802 ] } }, { email: 1 }) 

MongoDB Shell Result:

{ "_id" : ObjectId("5703f3966b3eb3d63f0e5610"), "email" : "Candice_Stoltenberg83@yahoo.com", "geo" : { "lat" : 33.06200408, "lng" : -8.06950717 } }
{ "_id" : ObjectId("5703f3966b3eb3d63f0e55db"), "email" : "Jessyca_Kling@hotmail.com", "geo" : { "lat" : 33.2303919, "lng" : -7.94778447 } }
{ "_id" : ObjectId("5703f3966b3eb3d63f0e55e8"), "email" : "Aric.Ratke17@hotmail.com", "geo" : { "lat" : 33.19421813, "lng" : -7.93873952 } }
...

Mongoose Query:

User.find({ geo: { $near:  [ 37.33259551999998, -122.03031802 ]   } })

Mongoose query always throws the [Error: Can't use $near]

@MaarekElets, I was not doing any save hooks for the type, for the index, I have already execute the index command in Mongo Shell

Mongo Shell index:

db.users.ensureIndex({geo:"2d"})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
}

@Iliyass
Copy link

Iliyass commented Apr 6, 2016

I had to rewrite the schema, I think Mongoose doesn't support { lat: Number, lng: Number }.

@vkarpov15
Copy link
Collaborator

@Iliyass what does your schema look like? Typically when a query works in the shell but not in mongoose it's related to the schema setup.

@vkarpov15 vkarpov15 modified the milestones: 4.4.13, 4.4.12 Apr 7, 2016
@vkarpov15 vkarpov15 removed this from the 4.4.13 milestone Apr 21, 2016
@vkarpov15
Copy link
Collaborator

Issue's gone stale, closing.

@nasr18
Copy link

nasr18 commented Apr 22, 2016

your schema is wrong. I'm using geospatial in my project and its working perfect. Here is my code.

geo: { type: [Number], index: "2d" }

Hope this will help you. @vkarpov15

@angelo0000
Copy link
Author

angelo0000 commented Apr 22, 2016

What do I need to provide in addition to my original issue? I have tried the shell query and it works just fine. I have tried two mongoose approaches as outlined in my original ticket and both fail with the same error. I am using mongoose v4.4.4 and mongo v3.0.7.

Shell query that works in RoboMongo:

db.rentallocations.find({ geo: { $near: { $geometry: { type: "Point", coordinates: [-121.31, 51.79] } } } })

Mongoose query that fails with error

models.rentalLocation.where('geo').near({ center: [-121, 51]}).exec(cb)

cb is just a simple callback to print the returned results

@angelo0000
Copy link
Author

@vkarpov15 I used your script above and it works out of the box. However I modified your script to be more like what our app does. We have our geo setup as a mongoose plugin so it gets injected as its own schema. When we do this, we get the error. Here is the modified script to show the error.


var assert = require('assert');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/gh4014');
mongoose.set('debug', true);

var schema = new mongoose.Schema({
  placeName: String
});

var geoSchema = new mongoose.Schema({
    type: {
      type: String,
      enum: 'Point',
      default: 'Point'
    },
    coordinates: {
      type: [Number],
      default: [0, 0]
    }
});

schema.add({ geo: geoSchema });
schema.index({ geo: '2dsphere' });

var MyModel = mongoose.model('gh4014', schema);

MyModel.
  where('geo').near({ center: [50, 50] }).
  exec(function(error) {
    assert.ifError(error);
    console.log('done');
    process.exit(0);
  });

@vkarpov15 vkarpov15 reopened this Apr 22, 2016
@vkarpov15 vkarpov15 added priority and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Apr 22, 2016
@vkarpov15 vkarpov15 added this to the 4.4.14 milestone Apr 22, 2016
@vkarpov15
Copy link
Collaborator

Thanks for the repro instructions @angelo0000, I'm on it :)

@vkarpov15
Copy link
Collaborator

Looks like this was just an instance of a missing query operator for single embedded subdocs, much like #4044. Will be fixed when 4.4.14 is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants