微信小程式雲資料庫關於單條記錄陣列欄位頭部追加資料問題——unshift函式的用法
js程式碼:
const db = wx.cloud.database();
const _ = db.command
db.collection("questions").doc(that.data.id).update({
data: {
comments: _.unshift({
comment: e.detail.value.comment,
time: getCurrentTimeUtil.formatTime(new Date()),
avatarUrl: that.data.userInfo.avatarUrl,
nickName: that.data.userInfo.nickName,
})
},
success: function(res) {
wx.showToast({
title: '釋出評論成功',
})
},
fail:function() {
}
})
測試 新增記錄順序依次是(與儲存到資料庫的順序相反):
"3":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"塞下秋來風景異","nickName":"傅華濤","time":"2018.10.26 08:30:38"}
"2":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"落霞與孤鶩齊飛,秋水共長天一色","nickName":"傅華濤","time":"2018.10.26 08:33:03"}
"1":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"空山新雨後,天氣晚來秋","nickName":"傅華濤","time":"2018.10.26 08:33:24"}
"0":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"春花秋月何時了","nickName":"傅華濤","time":"2018.10.26 08:55:16"}
雲資料庫json資料:
"_id":W9G7Cgfdgfdgfgfgf
"_openid":gfgfgfdgfdgdfgdfdgfg
"avatarUrl":https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf
"comments":
"0":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"春花秋月何時了","nickName":"傅華濤","time":"2018.10.26 08:55:16"}
"1":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"空山新雨後,天氣晚來秋","nickName":"傅華濤","time":"2018.10.26 08:33:24"}
"2":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"落霞與孤鶩齊飛,秋水共長天一色","nickName":"傅華濤","time":"2018.10.26 08:33:03"}
"3":{"avatarUrl":"https://wx.qlogo.cn/mmopen/gfdgfdgfgfgdgdfgf","comment":"塞下秋來風景異","nickName":"傅華濤","time":"2018.10.26 08:30:38"}
"nickName":傅華濤
"question":請寫出含有“秋”的詩句
"time":2018/10/25 20:46:00