我试图在特定位置附近随机获取20种食物。当我使用它时可以工作,但是如何在$ near中使用聚合 架构:
> select Count,Ts from test1 limit 30;
name: sensor1
time Count Ts
---- ----- --
1594003971238615143 1 1594003959534
1594003971238615143 10 1594003960446
1594003971238615143 100 1594003969537
1594003971238615143 101 1594003969638
1594003971238615143 102 1594003969739
1594003971238615143 103 1594003969840
1594003971238615143 104 1594003969941
1594003971238615143 105 1594003970042
1594003971238615143 106 1594003970143
1594003971238615143 107 1594003970244
1594003971238615143 108 1594003970345
1594003971238615143 109 1594003970446
1594003971238615143 11 1594003960547
1594003971238615143 110 1594003970547
1594003971238615143 111 1594003970648
1594003971238615143 112 1594003970749
1594003971238615143 12 1594003960648
1594003971238615143 13 1594003960749
1594003971238615143 14 1594003960850
1594003971238615143 15 1594003960951
1594003971238615143 16 1594003961053
1594003971238615143 17 1594003961154
1594003971238615143 18 1594003961255
1594003971238615143 19 1594003961356
1594003971238615143 2 1594003959638
1594003971238615143 20 1594003961457
1594003971238615143 21 1594003961558
1594003971238615143 22 1594003961659
1594003971238615143 23 1594003961760
1594003971238615143 24 1594003961861
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" });
MongoError:在此上下文中不允许$ geoNear,$ near和$ nearSphere 如何在$ near猫鼬附近使用集合函数来在特定位置附近获取随机食物
答案 0 :(得分:1)
您能尝试这样的事情吗?
Food.aggregate
([
{
$geoNear: {
near: { type: "Point", coordinates: [13.3339, 80.1943] },
distanceField: "dist.calculated",
maxDistance: 10000,
query: { "isEnabled": true }
}
}
])
在聚合框架中,$geoNear
应该是管道中的第一个管道,您可以使用query
来过滤结果,而不必使用match
。