天天看點

微信小程式 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