Reputation: 3109
i'm trying to use the $near operator with $find(), however i can't managed to get it done. i have tried 2 methods
userSchema
var userSchema = new db.Schema({
email: { type: String, unique: true, lowercase: true },
password: { type: String, select: false },
company_name: String,
location_verified: { type:Boolean, default:false},
account_verified: { type:Boolean, default:false},
date_joined: {type:Date, default:Date.now},
business_details: {
business_phone: String,
business_email: String,
business_location:[]
}
})
//index as 2d
userSchema.index({ 'business_detail.business_location': '2d' });
var User = db.model('User', userSchema);
Method 1
var limit = req.query.limit || 10;
var maxDistance = req.query.distance || 8;
maxDistance /= 6371;
var coords = [];
coords[0] = 101.6833;
coords[1] = 3.1333;
User.find({
'business_details.business_location': {
$near: coords,
$maxDistance: maxDistance
}
}).limit(limit).exec(function(err, locations) {
if (err) {
console.log("The ERROR:"+err);
}
console.log("The RESULT:"+locations);
});
Method 2
var limit = req.query.limit || 10;
var maxDistance = req.query.distance || 8;
maxDistance /= 6371;
var coords = [];
coords[0] = 101.6833;
coords[1] = 3.1333;
User.find({
'business_details': {
$near:{
$geometry:{'business_location':coords},
$maxDistance: maxDistance
}
}
}).limit(limit).exec(function(err, locations) {
if (err) {
console.log("The ERROR:"+err);
}
console.log("The RESULT:"+locations);
});
i have check my db index, the field i'm trying to use $near is having 2d index
> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "clairvy.users" }
{ "v" : 1, "unique" : true, "key" : { "email" : 1 }, "name" : "email_1", "ns" : "clairvy.users", "background" : true, "safe" : null }
{ "v" : 1, "key" : { "business_detail.business_location" : "2d" }, "name" : "business_detail.business_location_2d", "ns" : "clairvy.users", "background" : true, "safe" : null }
this is how my document look like
"_id" : ObjectId("557ae162d3fb543275135f04"),
"company_name" : "john inc",
"email" : "[email protected]",
"password" : "$2a$10$SDAXG8TrqjJIbvyGjpyQQuJDMTTDjMlNdpNQ9brsf4otGKr/CqI5i",
"business_details" : {
"business_phone" : "011112",
"business_email" : "[email protected]",
"business_fb" : "https://www.youtube.com/watch?v=NA4otP-v6iI",
"business_about_us" : " asd sad sad sadasd",
"business_tags" : [
{
"name" : "Marina Augustine",
"email" : "[email protected]",
"image" : "http://lorempixel.com/50/50/people?0",
"_lowername" : "marina augustine"
},
{
"name" : "Oddr Sarno",
"email" : "[email protected]",
"image" : "http://lorempixel.com/50/50/people?1",
"_lowername" : "oddr sarno"
}
],
"business_location" : [
101.6867332275391,
3.1285006558498596
],
"business_price_range" : 2,
"business_preparation_time_range" : 2
}
both methods give me back the same results which is the error "MongoError: n/a"
may i know which part i have make a mistake ?
your help is appreciated, thanks
Upvotes: 0
Views: 154
Reputation:
Your index is on the wrong namespace. What you have:
{ "business_detail.business_location" : "2d" },
What it should be:
{ "business_details.business_location" : "2d" },
So the correct field here is "business_details", correct with:
db.users.ensureIndex({ "business_details.business_location": "2d })
Or otherwise define that index in your mongoose schema. But also remove any other incorrectly named indexes on the collection as some "geo" commands get confused by multiple indexes.
db.users.dropIndex({ "business_detail.business_location": "2d" })
Since MongoDB is "schemaless" there is no error that would be produced from adding an index that does not exist in your document. As "schemaless" there is no way of MongoDB itself knowing that "some day" or in "some document" that data might exist.
It's good practice to add to your schema definition so there is some point in your code that reflects what you intend:
userSchema.index({ "business_details.business_location": "2d" });
Upvotes: 3