Need help with geoSearch and geoNear (getting error "can't find ns").

868 views
Skip to first unread message

dr_jonat...@hotmail.co.uk

unread,
Feb 4, 2015, 7:20:33 AM2/4/15
to mongoo...@googlegroups.com
I am new to mongoose and am trying to use some of the geo-indexing functions through Mongoose (3.8.22).  Below is a very simple test script and the database is correctly created with an index having a value of "ns": "geojsonTest.places" (this is what I expect from the code below). However, when I run the script, the geoNear query I get the error "MongoError: can't find ns".  I get the same thing when I use a geoHaystack index together with a geoSearch query.  PLease can somebody tell me what I am doing wrong.  Thank you!

//test.js
var mongoose = require('mongoose');
mongoose
.connect('localhost','geojsonTest');
var Schema = mongoose.Schema;


//--------------------------------------------------------------------
var places = new Schema({
 
'geom': {
 
'type' : { type: String, default: "Point" },
 
'coordinates': [{type: "Number"}]
 
},
 
'pointType': String,
 
'name': String,
 
'comment': String
});


//set a geoHaystack index for optimised small-area searches
//placesSchema.index({'coordinates' : 'geoHaystack', 'pointType' : 1},{bucketSize : 1});  // also results in same error with geoSearch
places
.index({'coordinates' : '2dsphere'});


var Places = mongoose.model('Places', places);


// define some dummy data
var data = [
 
{ pointType: 'Place', name: 'Little Snoring', comment: 'Bogus, Dude!', geom: { type: 'Point', coordinates: [-20.0, 5.0] }},
 
{ pointType: 'Place', name: 'Fantasia Hollow', comment: 'Awesome!', geom: { type: 'Point', coordinates: [6.0, 10.0] }},
 
{ pointType: 'Place', name: "Lion's Den", comment: 'Growl-tastic', geom: { type: 'Point', coordinates: [34.0, -50.0] }},
 
{ pointType: 'Place', name: 'Dun Roamin', comment: 'So unoriginal', geom: { type: 'Point', coordinates: [-100.0, 70.0] }},
 
{ pointType: 'Sausage', name: 'Floodsville', comment: 'No way (sad face)', geom: { type: 'Point', coordinates: [38.0, 38.0] }}
];


data
.forEach(function(item){
 console
.log('Loading item: ' + item['name']);
 
Places.create(item, function (err, small) {
 
if (err) return handleError(err);
 
});
});


var testPoint = { type : "Point", coordinates : [-20.0, 5.0] };
Places.geoNear(testPoint, { maxDistance : 25, spherical : true }, function (err, results, stats) {
 
if (err) throw err;
 console
.log('Result: ' + results);
});




dr_jonat...@hotmail.co.uk

unread,
Feb 4, 2015, 11:40:47 AM2/4/15
to mongoo...@googlegroups.com
UPDATE: I have tried manually creating a geoHaystack index using the Mongo Client shell.  This works (and also works fine in my code - I just did it manually to be sure).  I then manually ran a geoSearch command following the Mongo cannonical example and get an error "no geoSearch index".  So... I suspect we are getting nearer the heart of the matter... but... shouldn't Mongo be looking for a geoHaystack index for a geoSearch query (as per the documentation).  I am beginning to suspect that it is neither my code nor mongoose which is at fault but that would mean this be a bug in Mongo itself.  

Help on this will be definitely appreciated.
Reply all
Reply to author
Forward
0 new messages