uni-cloud使用了聚合操作与另外一个表的联表查询做法

本意是要在获取离当前定位一定距离的数据,并且需要从uni-id-users表中获取这些数据对应的头像,但是直接通过collection联表是查不了的,会报错。因为聚合操作不支持这样做。

最后折腾了很多种方式只能这样做,因为geonear必须是聚合操作的第一阶段,在geonear之后也接不了where,filed等这些条件。

	// 根据定位查询店铺信息
			async queryShopsByPosition(params) {
				console.log('(parseInt(this.pickerArray[this.pickerIndex]) || 10) * 1000', (parseInt(this.pickerArray[
					this.pickerIndex]) || 10) * 1000)

				try {
					const userLocation = await getUserLocation();
					console.log('userLocation', userLocation.longitude, userLocation.latitude);
					const db = uniCloud.database();
					const geoNearResult = await db.collection('opendb-poi').aggregate().geoNear({
						distanceField: 'distance',
						spherical: true,
						near: new db.Geo.Point(userLocation.longitude, userLocation.latitude), // 人民英雄纪念碑
						maxDistance: (parseInt(this.pickerArray[this.pickerIndex]) || 10) * 1000,
					}).end();

					const userIds = (geoNearResult?.result?.data || []).map(item => item.user_id);
					console.log('userIds', userIds);
					const avatarResults = await db.collection('uni-id-users').where({
						_id: db.command.in(userIds)
					}).field('_id,avatar_file').get();
					console.log('avatarResults', avatarResults);
					// 将两个查询结果合并  
					let combinedResults = (geoNearResult?.result?.data || []).map(poi => {
						let avatar = (avatarResults?.result?.data || []).find(avatar => avatar._id === poi
							.user_id);
						return {
							...poi,
							avatar_file: avatar ? avatar.avatar_file : null
						};
					});
					// 解析出当前定位的地址
					// let uniMapObj = uniCloud.importObject("uni-map-co")
					// let res2 = await uniMapObj.location2address({
					// 	location: `${userLocation.latitude},${userLocation.longitude}`,
					// })
					// console.log('res_position', combinedResults);
					// if (combinedResults?.result?.errCode === 0) {
					this.shops = combinedResults;
					this.shopsPosition = combinedResults;
					console.log('this.shops', this.shops);
					// }
				} catch (e) {
					uni.showToast({
						title: "查询出错了",
						icon: "error"
					})
				}
			}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值