如何在Firebase中将时间戳分配给数组类型doc项 [英] How to assign a timestamp to an array type doc item in Firebase

查看:50
本文介绍了如何在Firebase中将时间戳分配给数组类型doc项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Vue.js应用程序,该应用程序可以将项目动态推送到Cloud Firestore中的数组中.这些项目位于 events 数组中,其中包含各种对象,其中之一是时间戳.参见下面的功能:

I am attempting to build a Vue.js app that enables dynamic pushing of items into an array in Cloud Firestore. The items are in the events array, contains various objects, one of which is a timestamp. See function below:

let newDetails = this.newDetails
let newImage = this.imageUrl
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,
                    'image': newImage,
                    'timestamp': moment(Date.now()).format('lll')
                })
            })
        })
    })

我的目标是根据时间戳的顺序将每个数组项呈现给DOM.我最初尝试设置以下Vuex操作(再次参见下文)以使用 .orderBy()函数以实现此目标:

My goal is to render each array item to the DOM, based on the order of the timestamps. I initially tried setting up the following Vuex action (see below again) to use an .orderBy() function in order to achieve this goal:

setCurrentUser: async context => {
    let ref = db.collection('users').orderBy('events')
    let snapshot = await ref.where('user_id', '==', firebase.auth().currentUser.uid).get()
    const users = []
    snapshot.forEach(doc => {
        let currentUserData = doc.data()
        currentUserData.id = doc.id
        users.push(currentUserData)
    })
    context.commit('setCurrentUser', users)
},

但是,我了解可能无法基于数组类型doc项中的时间戳来订购这些项.关于如何在Firestore中为每个数组项分配时间戳而不将时间戳分配为数组对象的任何建议,以便仍可以基于时间戳来安排数组类型doc项?

However, I understand that it might not be possible to order these items based on timestamps inside array type doc items. Any recommendations on how to assign a timestamp to each array item in Firestore without assigning timestamp as an array object, so that the array type doc items can still be arranged based on timestamp?

推荐答案

以下是Cloud Firestore的可用数据类型:

Here are the available data-types for Cloud Firestore: https://firebase.google.com/docs/firestore/manage-data/data-types

我建议使用 Map 解决基于时间戳对事件进行排序的问题.

I would suggest using a Map to solve the problem of sorting the events based on the timestamp.

选项1: Map< Timestamp,Event> 如果同一时间没有事件,则键可以是时间戳.之所以将键设置为 Timestamp ,是因为 Map 是按 Key 然后按Cloud的 Value 排序的消防站.

Option 1: Map<Timestamp, Event> The key can be Timestamp if there can be no events in the same time. The reason for setting the key as Timestamp is because the Map is sorted by Key and then by Value by Cloud Firestore.

选项2:.如果一个用户同时可以有多个事件,则键可以是 Event .因此,地图看起来像这样的 Map< Event,Timestamp> ,并且您可以从Firestore检索 Map 后根据值进行排序.

Option 2: If there can be more than one event for a user at the same time, the key can be Event. So the map would look something like this Map<Event,Timestamp> and you can sort based on the values after retrieving the Map from Firestore.

选项3::如果您可以将所有事件存储在另一个集合 Events 中,则事件的 documentId 可以成为关键.因此, Map 将是 Map< String,Timestamp> .如果用户可以进行很多活动,我建议使用此选项.

Option 3: If you can store all the events in another collection Events, then the documentId of event can be the key. So the Map would be Map<String,Timestamp>. I would suggest this option if a user can have a lot of events.

希望这会有所帮助.

这篇关于如何在Firebase中将时间戳分配给数组类型doc项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆