javascript - Error when trying to query with $near in find() -
i'm trying use $near operator $find(), can't managed done. 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 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 db index, field i'm trying use $near 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 how document like
"_id" : objectid("557ae162d3fb543275135f04"), "company_name" : "john inc", "email" : "example@gmail.com", "password" : "$2a$10$sdaxg8trqjjibvygjpyqqujdmttdjmlndpnq9brsf4otgkr/cqi5i", "business_details" : { "business_phone" : "011112", "business_email" : "example@gmail.com", "business_fb" : "https://www.youtube.com/watch?v=na4otp-v6ii", "business_about_us" : " asd sad sad sadasd", "business_tags" : [ { "name" : "marina augustine", "email" : "m.augustine@exampleas.com", "image" : "http://lorempixel.com/50/50/people?0", "_lowername" : "marina augustine" }, { "name" : "oddr sarno", "email" : "o.sarno@exampleas.com", "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 same results error "mongoerror: n/a"
may know part have make mistake ? appreciated, thanks
your index on wrong namespace. have:
{ "business_detail.business_location" : "2d" },
what should be:
{ "business_details.business_location" : "2d" },
so correct field here "business_details", correct with:
db.users.ensureindex({ "business_details.business_location": "2d })
or otherwise define index in mongoose schema. remove other incorrectly named indexes on collection "geo" commands confused multiple indexes.
db.users.dropindex({ "business_detail.business_location": "2d" })
since mongodb "schemaless" there no error produced adding index not exist in document. "schemaless" there no way of mongodb knowing "some day" or in "some document" data might exist.
it's practice add schema definition there point in code reflects intend:
userschema.index({ "business_details.business_location": "2d" });
Comments
Post a Comment