我正在尝试将$ near运算符与$ find()一起使用,但是我无法完成它。我尝试了2种方法
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);
方法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);
});
方法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);
});
我已经检查了我的数据库索引,我试图使用$ near的字段有2d索引
> 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 }
这就是我的文档的样子
"_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
}
这两种方法都给了我相同的结果,这就是错误"MongoError: n/a"
我可能知道哪个部分犯了错误?
感谢您的帮助,谢谢
答案 0 :(得分:3)
您的索引位于错误的命名空间中。你有什么:
{ "business_detail.business_location" : "2d" },
应该是什么:
{ "business_details.business_location" : "2d" },
因此,此处的正确字段为" business_detail s ",更正为:
db.users.ensureIndex({ "business_details.business_location": "2d })
或者在你的猫鼬模式中定义该索引。但是也要删除集合上任何其他错误命名的索引,因为某些" geo"命令被多个索引搞糊涂了。
db.users.dropIndex({ "business_detail.business_location": "2d" })
因为MongoDB是"无模式的"添加文档中不存在的索引不会产生任何错误。作为"无模式" MongoDB本身没有办法知道"某天"或者在某些文件"这些数据可能存在。
添加到您的架构定义是一种很好的做法,因此您的代码中有一些内容可以反映您的意图:
userSchema.index({ "business_details.business_location": "2d" });