天天看点

微信小程序 lookup 联表查询

微信小程序 云开发

store 商品集合

_id: 'a8831daa5fef02f50153146e5902c2aa',
openid: 'oMlDj5JiYiwxyBJm-d4JFY-Ov-LM', // 发布商品者 openid
title: '小米手电筒',
liulan: 29,
price: 19.9,
imgUrl: 'xxxxxxxxxxxxxxxx',
time: 1609499381638,
shenhe: true,

           

fav 收藏集合

_id: 'b45a21d55ff155e2036603d6218fa777',
_openid: 'oMlDj5JiYiwxyBJm-d4JFY-Ov-LM',	// 收藏者 openid
fav_id: 'a8831daa5fef02f50153146e5902c2a1'	// 收藏的商品 _id

           

fav 集合记录数量 = store 商品集合记录数量 * 所有商品总收藏数量

会产生 超级多 的记录

联表查询 (我收藏的.wxml)云函数代码

function getfavlist(event, context) {
	return new Promise(function (resolve, reject) {
		db.collection('fav').aggregate()
			.lookup({
				from: 'store',
				localField: 'fav_id',
				foreignField: '_id',
				as: 'storedetail',
			})
			.match({
				_openid: event.userInfo.openId
			})
			.end()
			.then(res => {
				console.log(res);
				resolve(res);
			})
			.catch(err => console.error(err))
	})
}

           
微信小程序 lookup 联表查询
微信小程序 lookup 联表查询

感觉把 微信小程序 云开发 文档型数据库 用成了关系型数据库

还可以给 store 商品集合 下的所有记录 新增 fav_openid 字段 (一个数组存储 收藏者的 openid)

_id: 'a8831daa5fef02f50153146e5902c2aa',
openid: 'oMlDj5JiYiwxyBJm-d4JFY-Ov-LM', // 发布商品者 openid
fav_openid: ['小明的 openid', 'Tom 的 openid', '马保国的 openid'],
title: '小米手电筒',
liulan: 29,
price: 19.9,
imgUrl: 'xxxxxxxxxxxxxxxx',
time: 1609499381638,
shenhe: true,

           

这样看上去 store 商品集合存储空间会比较大?

试试这样?

fav 收藏集合

_id: 'b45a21d55ff155e2036603d6218fa777',
_openid: 'oMlDj5JiYiwxyBJm-d4JFY-Ov-LM',	// 收藏者 openid
fav_openid: ['小明的 openid', 'Tom 的 openid', '马保国的 openid'],

           

fav 集合记录数量 = store 商品集合记录数量

喜欢或对你有帮助,请点个赞吧 。

有错误或者疑问还请评论指出。

我的个人网站 --> 点击访问 。

END