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

Using the canonical 'getNear' example returns MongoError "can't find ns" #2653

Closed
mappagnosis opened this issue Feb 6, 2015 · 2 comments
Closed
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@mappagnosis
Copy link

I have read this issue (#2313) and ensured that I am not repeating the mistake.

After several attempts to get both geoNear and geoSearch working, I set up a test using the cannonical example in the Mongoose release notes for v 3.8 (I am using 3.8.22 as v4 is marked as unstable) here: https://github.com/LearnBoost/mongoose/wiki/3.8-Release-Notes#geojson-support-for-querynear as follows:

var mongoose = require('mongoose');
mongoose.connect('localhost','geojsonTest');
var Schema = mongoose.Schema;
var schema = new Schema({
pos: [Number],
type: String
});

schema.index({'pos' : '2dsphere'});
Geo = mongoose.model('Geo', schema);

var data = [
{ type: 'Point', pos: [10.0, 5.0] },
{ type: 'Point', pos: [9.1, 9.0] },
{ type: 'Point', pos: [9, -50.0] },
{ type: 'Point', pos: [-100.0, 70.0] },
{ type: 'Point', pos: [42.0, 38.0] }
];

data.forEach(function(item){
Geo.create(item);
});

Geo.geoNear([9,9], { spherical : true, maxDistance : .1 }, function (err, results, stats) {
if (err) throw err;
console.log('Result: ' + results);
});

Looking in the Mongo shell, this loads the data correctly. The command db.system.indexes.find() returns the following:
{ "v" : 1, "key": { "_id" : 1 }, "name" : "id", "ns" : "geojsonTest.geos" }

Listing my collections with db.getCollectionNames() gives me:
[ "geos", "system.indexes" ]

Notice how the collections and indexes list 'geos' when I have defined the model name as 'Geo'. I get a similar thing if I substitute 'Place' for 'Geo' (the collection is called 'places'). But if I call the model 'Places' the collection does not get created as 'placess' (two 's'). I don't know if this is deliberate or has any bearing on the issue. It just seems odd and inconsistent. Also notice that no index seems to be created on 'pos' (unless I misunderstand the output of db.system.indexes.find() ).

I get the same result ("can't find ns") whether I use legacy coordinate pairs or geoJson. Since I am using the example provided in the release notes, I must assume this is a bug.

@mappagnosis mappagnosis changed the title Using the cannonicl 'getNear' example returns MongoError "can't find ns" Using the cannonical 'getNear' example returns MongoError "can't find ns" Feb 6, 2015
@mappagnosis mappagnosis changed the title Using the cannonical 'getNear' example returns MongoError "can't find ns" Using the canonical 'getNear' example returns MongoError "can't find ns" Feb 6, 2015
@vkarpov15
Copy link
Collaborator

The reason for "geos" is that mongoose has some pluralization rules for model names by default. Unfortunately its not well documented. You can specify a collection option in your schema or specify a third argument to .model(), for instance:

var Geo = mongoose.model('Geo', geoSchema, 'geo');

And then mongoose will use the collection named "geo" rather than "geos".

As for your error, since the index isn't being created, that would explain why the geoNear() call is failing. Can you attach a listener to the Geo model's index event?

Geo.on('index', function(err) {
  console.log('Finished building indexes, error: ' + err);
});

And see if that returns an error?

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Feb 6, 2015
@mappagnosis
Copy link
Author

Aha! Thanks for the heads up on the pluralization! That explains that issue. The problem with the index not getting created turned out to be very simple in the end and nothing to do with either Mongo or Mongoose. It was simply node's asynchronous environment meant that populating the database with dummy data, index it and querying it was all effectively happening simultaneously. In a production environment this will actually be less likely, but it is a good object lesson nonetheless as I can trap that eventuality by using Node's async middleware.

Many thanks for your help - this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants