-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
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 |
Yes I was able to execute the same query in monghub actually. |
Can you clarify which version of mongoose you're using, and show me the output from running your query with mongoose debug mode on? |
@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 |
@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
can I still make a geospatial based on this schema, because it works in MongoDb shell ? |
What does the result of the geospatial query look like in the shell? |
Are either @Iliyass or @angelo0000 using something like this to ensure that the geo type is always set to "Point"?
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. |
@vkarpov15 it looks normal, like any find, if I didn't set the
MongoDB Shell Result:
Mongoose Query:
Mongoose query always throws the @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:
|
I had to rewrite the schema, I think Mongoose doesn't support |
@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. |
Issue's gone stale, closing. |
your schema is wrong. I'm using geospatial in my project and its working perfect. Here is my code.
Hope this will help you. @vkarpov15 |
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 |
@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.
|
Thanks for the repro instructions @angelo0000, I'm on it :) |
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. |
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:
The above will return: [Error: Can't use $near]
I have also tried the following:
It will also result in the same error: [Error: Can't use $near]
The text was updated successfully, but these errors were encountered: