【发布时间】:2020-07-06 13:34:04
【问题描述】:
我试图在特定位置附近随机获取 20 种食物。当我使用 find 它有效但如何使用 $near 的聚合 架构:
const foodSchema = new Schema({
foodName: {
type: String,
required: true
},
image: {
type: String,
required: true
},
price: {
type: String,
required: true
},
shopName: {
type: String,
required: true
},
isVerified:{
type:Boolean,
default:false,
required:true
},
isEnabled:{
type:Boolean,
default:false,
required:true
}
,
location: {
type: { type: String },
coordinates: [],
},
shopId:{
type: Schema.Types.ObjectId,
ref: 'Shop',
required: true
}
});
foodSchema.index({ location: "2dsphere" });
Food.aggregate([{$match:{isEnabled:true,location: {
$near: {
$maxDistance: 10000,
$geometry: {
type: "Point",
coordinates: [13.3339, 80.1943]
}
}
}}}])
.then(data=>{
console.log(data);
res.render('user/home',{
pageTitle:"UiMart",
foodlist:data
});
})
.catch(err=>{
console.log(err);
res.send("err");
});
MongoError:在此上下文中不允许使用 $geoNear、$near 和 $nearSphere 如何使用 $near mongoose 的聚合函数来获取特定位置附近的随机食物
【问题讨论】:
标签: node.js mongodb express mongoose