skip to Main Content

I want to save data on Firebase Firestore where the key will be timestamp:

await FirebaseFirestore.instance
      .collection("users")
      .doc(uid)
      .collection('data')
      .doc(device.id)
      .update({
    DateTime.now().toString(): "${result['humidity']}_${result['tempInC']}"
  });
}

What can be the possible reasons for this nested hierarchy as shown in the image: Screenshot, any solution?

2

Answers


  1. Chosen as BEST ANSWER

    Using milli_seconds_since_epoch is better alternative in such scenarios.


  2. According to my first question in the comments section:

    Do you need to save a Date object as a key inside a document in Firestore?

    And your answer:

    yeah @AlexMamo

    Please note that there is no way you can save a Date/Timestamp object as a key in a Firestore document. The keys are always strings. You can indeed convert the Date object into a String, but I don’t think this is what you’re looking for.

    The best option I can think of would be to save the timestamp as a value and not as a key. For example, you can add a field called addedAt that should hold a Firestore timestamp. In this way, you’ll be able to query the collection using Timestamp objects.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search