【发布时间】:2022-01-21 18:45:20
【问题描述】:
我将 NestJS 与 Mongoose 一起使用,我有 2 个集合、区域和诊所
首先我搜索用户附近的地区
async findAllByLocation(location: Location) {
const territories = await this.territoryModel.find({
location: {
$near: {
$geometry: {
type: 'Point',
coordinates: [location.lat, location.long],
},
},
},
});
return territories.map((territory) => {
return {
_id: territory._id,
name: territory.name,
};
});
}
然后诊所服务利用此函数从返回的数组中获取具有任何区域的诊所
async getClinicsByLocation(location: any): Promise<any> {
const territories = await this.territoryService.findAllByLocation(location);
const clinics = await this.clinicModel.find({
territory: { $in: territories.map((t) => t._id) },
});
return clinics;
}
我现在的问题是区域数组是从最近到最远排序的,但是诊所是随机顺序的,因此诊所在响应中的领土比其他诊所更远
一种解决方案是在每个区域单独建立诊所,然后合并阵列,但这既不节省时间,也不节省性能
有什么想法吗?
【问题讨论】:
标签: typescript mongodb mongoose