Search code examples
javascriptfirebasevue.jsgoogle-cloud-firestorevuex

How to assign a timestamp to an array type doc item in Firebase


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')
                })
            })
        })
    })

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)
},

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?


Solution

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

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

    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.

    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.

    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.

    Hope this helps.