3

Simple question:
what is the proper way of making a geo-spatial query with mongoose on nodejs?

Complex story:
I created a Schema with mongoose with a spatial index

var MySchema = newSchema({
// skipped ...
    location : {
        type : [ Number ],
        required : true,
        index : '2d'
    },
// skipped ...
});

Then I inserted into the table 3 documents. I can retrieve these documents with both mongo client and mongoose using db.mymodels.find({}) and MyModel.find({});

Now I try to make a geo-spatial query with a bounding box.
mongo client works ok

db.mymodels.find({location: {$within: {$box: box}}})

returns only one result as expected, within the box.
Mongoose on the other hand, returns back all three results

query = MyModel.where({location: {$within : {$box : box}}});
query.run(cb)

This question says you should use find() instead of where, but when I tried replacing find with where, I got an error 'need an area > 0' I checked, my bounding box is formatted correctly, lower-left then upper-right.

What to do? What is the proper way to make a geo-spatial query with a bounding box using mongoose?

Community
  • 1
  • 1
Paul
  • 1,757
  • 2
  • 11
  • 21

1 Answers1

0

It doesn't matter if you use find() or where().

Would you mind posting what mongoose is sending to the db by enabling logging and checking your console: mongoose.set('debug', true)

aaronheckmann
  • 10,625
  • 2
  • 40
  • 30
  • Thank you Thank you Thank you! I've noticed that my coordinate array consists of strings instead of numbers '78.0' instead of 78, I didn't notice it during usual debug, only now with your suggestion! – Paul May 08 '12 at 15:16
  • I use node-inspector, which debugs nodejs server-apps using chrome's developer tools, I just noticed that numbers are shown in blue while strings in red, but strings don't have any ' or " chars around them. Damn it! – Paul May 09 '12 at 13:47