Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firestore子集合删除文档中最旧的_Javascript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript Firestore子集合删除文档中最旧的

Javascript Firestore子集合删除文档中最旧的,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我一直在努力弄清楚如何从子集合中删除文档,只保留最新的5个文档 我首先尝试获取子集合中的文档列表,按'updated'日期时间戳排序。但是,它返回null let updates = await firestore .collection('spots') .doc(spot.id) .collection('spotupdates') .orderBy('updated','desc'); 然后我尝试从列表中删除最老的,以确保只剩下5个 var cntr =

我一直在努力弄清楚如何从子集合中删除文档,只保留最新的5个文档

我首先尝试获取子集合中的文档列表,按
'updated'
日期时间戳排序。但是,它返回null

let updates = await firestore
    .collection('spots')
    .doc(spot.id)
    .collection('spotupdates')
    .orderBy('updated','desc');
然后我尝试从列表中删除最老的,以确保只剩下5个

var cntr = 0;
while(updates.docs.length > 5){
    await firestore
        .collection('spots')
        .doc(spot.id)
        .collection('spotupdates')
        .doc(updates[cntr].id)
        .delete();
    cntr++;
}
cntr = null;

请帮助-根据Firebase从数据库中删除数据的文档,确实卡住了

要在Cloud Firestore中删除整个集合或子集合, 检索集合或子集合中的所有文档,然后 删除它们。如果有较大的集合,则可能需要删除 以较小的批处理文档,以避免内存不足错误。重复 删除整个集合之前的过程,或 次收集

您可以在中找到一个简化代码片段来删除集合和子集合

您还将在中找到有关删除Firestore中的字段、文档和集合的更多信息和详细示例


让我知道这是否有用

根据Firebase从数据库中删除数据的文档:

要在Cloud Firestore中删除整个集合或子集合, 检索集合或子集合中的所有文档,然后 删除它们。如果有较大的集合,则可能需要删除 以较小的批处理文档,以避免内存不足错误。重复 删除整个集合之前的过程,或 次收集

您可以在中找到一个简化代码片段来删除集合和子集合

您还将在中找到有关删除Firestore中的字段、文档和集合的更多信息和详细示例


让我知道这是否有用

我在我的应用程序上做了类似的事情,但基于您的数据结构

首先,获取集合中的所有文档

const updates = await firestore
                 .collection('spots')
                 .doc(spot.id)
                 .collection('spotupdates')
                 .get()
映射文档数据以返回数组

const updatesArray = updates.docs.map(doc => doc.data())
然后删除逻辑

if (updatesArray.length > 5){
            const sorted = updatesArray.sort((a,b) => b.created_at - a.created_at)
            const oldest = sorted.pop()
            await firestore
             .collection('spots')
             .doc(spot.id)
             .collection('spotupdates').doc(oldest.id).delete()
        }

我在我的应用程序上做了类似的事情,但基于您的数据结构

首先,获取集合中的所有文档

const updates = await firestore
                 .collection('spots')
                 .doc(spot.id)
                 .collection('spotupdates')
                 .get()
映射文档数据以返回数组

const updatesArray = updates.docs.map(doc => doc.data())
然后删除逻辑

if (updatesArray.length > 5){
            const sorted = updatesArray.sort((a,b) => b.created_at - a.created_at)
            const oldest = sorted.pop()
            await firestore
             .collection('spots')
             .doc(spot.id)
             .collection('spotupdates').doc(oldest.id).delete()
        }

非常感谢。我以前经历过所有这些,但在我目前的环境中没有帮助。我不会删除整个子集合,我也不担心超时等限制,因为集合中可能只有6个文档。orderby是其返回的第一个问题nulls@cfl您是否检查过时间戳字段实际上不是空的?其中一些字段可以是空的,但大多数字段都已设置。根据文档,那些为空的将不包括在过滤列表中,这是100%。但整个回答都是否定的,谢谢。我以前经历过所有这些,但在我目前的环境中没有帮助。我不会删除整个子集合,我也不担心超时等限制,因为集合中可能只有6个文档。orderby是其返回的第一个问题nulls@cfl您是否检查过时间戳字段实际上不是空的?其中一些字段可以是空的,但大多数字段都已设置。根据文档,那些为空的将不包括在过滤列表中,这是100%。但整个反应是无效的