【问题标题】:How to efficiently add items to collection with firebase firestore如何使用 Firebase Firestore 有效地将项目添加到集合中
【发布时间】:2018-03-29 11:31:15
【问题描述】:

我有一组标签要添加到 Firestore 集合中。

如果我没有误解我在这里的方法,我认为我正在对集合进行单独添加,而我认为将它们“分组”并一次设置它们会更有效。这样的事情可能吗?是否可以同时将文档添加到锻炼集合中?

现在我正在查看 tags.length + 1 每次调用此函数时都会写入 firebase。我想尽可能减少它。

logWorkoutAsync({ userId, timeStamp, tags }){
    var db = this.firebase.firestore();

    return db.collection('users').doc(userId).collection('workouts').add({
        timeStamp,
        'class': false
    }).then(doc => {

        var tagsCollection = doc.collection('tags')

        var promises = []

        tags.forEach(t => {
            promises.push(tagsCollection.doc(t.id.toString()).set(t))
        })

        return Promise.all(promises)
    })
}

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    Cloud Firestore 支持批量写入,请参阅此处的文档: https://firebase.google.com/docs/firestore/manage-data/transactions

    所以你可以这样做:

    logWorkoutAsync({ userId, timeStamp, tags }){
        var db = this.firebase.firestore();
    
        return db.collection('users').doc(userId).collection('workouts').add({
            timeStamp,
            'class': false
        }).then(doc => {
    
            var tagsCollection = doc.collection('tags');
    
            // Begin a new batch
            var batch = db.batch();
    
            // Set each document, as part of the batch
            tags.forEach(t => {
                var ref = tagsCollection.doc(t.id.toString());
                batch.set(ref, t);
            })
    
            // Commit the entire batch
            return batch.commit();
        })
    }
    

    【讨论】:

    • 哇!这太棒了!我也可以在批处理中包含初始的 .add 吗?也许如果我做类似 batch.set(db.collection('users').doc(userId).collection('workouts').doc(timeStamp.toString), { timeStamp, 'class': false }) 的事情?在这种情况下,我只需要提供一个“id”,对吧?
    • 是的,你绝对可以做到!您最多可以批量添加 500 个操作。而且您的直觉是正确的,要获得batch.add(),您需要在其他地方执行.doc() 以创建引用,然后在该引用上调用batch.set()
    • @hatboysam 如果我们想在一个批次中编写超过 500 个操作,我们该怎么办?可以多次连接吗?我的意思是,有 2 个批次,一个有 500 个操作,另一个有 250 个 .. 等等
    • @hatboysam 您如何“添加”到收藏而不是“设置”?用例:使用系统生成的密钥将多个文档添加到集合中。
    • @Amsakanna 我使用了 short-unique-id 包来生成一个“类似 firebase”的 id,这对我有用 const ref = db.collection('collection').doc(uid.randomUUID(20)); db.batch().set(ref, {});
    【解决方案2】:

    如果您想将多个文档添加到集合中,您可以执行类似的操作,这对我来说效果很好,因为我陷入了类似的情况。

    let colRef = db.collection('cars')
    
    /// Batch Thing //
    var batch = db.batch();
    
    let cars = [{name: 'Audi', model: 'A8'}, {name: 'BMW', model: '730'}]
    cars.forEach(c => {
      let ref = colRef.doc(`${c.name}`)
      batch.set(ref, {
        name: `${c.name}`,
        model: `${c.model}`
      })
    })
    
    return batch.commit()
      .then(data => {
        console.log('good')
      })
      .catch(error => {
        console.log('there is an error')
      })
    

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 2021-12-30
      • 1970-01-01
      • 2018-05-10
      • 2022-06-10
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      相关资源
      最近更新 更多