如何使用 .orderby() 按时间戳对 Firebase 数组中的项目进行排序



>我正在尝试使我的 Vue.js 应用程序从 Firestore 数据库中检索项目并按时间戳顺序排列这些项目。通常,我会使用:

let ref = db.collection('users').orderBy('timestamp')

。如果时间戳只是一个文档项目。但是,我设置了此应用程序,以便timestamp是名为events的 firebase 文档数组项中的对象。

如何设置上述语句以使用events数组文档项中的timestamp按时间戳排列我的项目?

更新的功能

let newDetails = this.newDetails
let timestamp = moment(Date.now()).format('lll')
let ref = db.collection('users').where('user_id', '==', firebase.auth().currentUser.uid)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id, ' => ', doc.data())
doc.ref.update({'events': firebase.firestore.FieldValue.arrayUnion({
'name': doc.data().name,
'details': newDetails,
'timestamp': moment(Date.now()).format('lll')
})
})
})
})

无法根据数组类型文档字段中的单个值对查询进行排序。您必须将此时间戳值提升到其自己的字段才能使用它执行排序。

最新更新