0

Firestore Database Image

So I am trying to make a chat app with cloud Firestore and I don't how to delete a document by timestamp.

This is currently how I delete a document.but when I tried it deletes all the document:

onLongPress: () async {
  await FirebaseFirestore.instance
      .collection('messages')
      .doc(
          groupChatId)
      .collection(
      groupChatId)
      .orderBy("timestamp",descending: true).get().then((value) => {
        for(DocumentSnapshot ds in value.docs){
          ds.reference.delete()
        }
  });
},
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

try this code:

await FirebaseFirestore.instance
      .collection('messages')
      .doc(
          groupChatId)
      .collection(
      groupChatId)
      .where("timestamp", isLessThan: DateTime.now().microsecondsSinceEpoch-TheNumberOfMicrosencundenTheDocumentCanBeOld).get().then((value) => {
        for(DocumentSnapshot ds in value.docs){
          ds.reference.delete()
        }
  });
liam spiegel
  • 520
  • 5
  • 18