批次500从json文件写入firestore循环

时间:2020-03-09 14:56:57

标签: node.js firebase google-cloud-firestore firebase-admin

使用从该线程https://stackoverflow.com/a/51070855/1701580中获得的一些启发,并回答我试图使我的循环正常工作,即将其批量写入firestore。但是以某种方式,即使我看到自己遍历数组中的不同值,我也只能更新1个文档。我将数据加载到数组中并从那里工作。

const db = admin.firestore();
const jsonStream = StreamArray.withParser();

let arr = []
jsonStream.on('data', ({ key, value }) => {
    arr.push(value);
});

jsonStream.on('end', () => {

    var counter = 0;
    var commitCounter = 0;
    var batches = [];

    arr.forEach((a, ind) => {
        batches[commitCounter] = db.batch();
        if (counter <= 498) {
            var thisRef = db.collection('Testing').doc(a.id);
            console.log("id")
            console.log(a.id);
            batches[commitCounter].set(thisRef, { ...a });
            counter = counter + 1;
        } else {
            counter = 0;
            commitCounter = commitCounter + 1;
            batches[commitCounter] = db.batch();
        }
    })
    for (var i = 0; i < batches.length; i++) {
        if(i==0)
        {
            console.log(batches[0])
        }
        batches[i].commit().then(function () {
            console.count('wrote batch');
        });
    }
});

const filename = path.join(__dirname, 'mydata.json');
fs.createReadStream(filename).pipe(jsonStream.input);

1 个答案:

答案 0 :(得分:2)

以下行在每次迭代时执行,这实际上是在每个回合中“重置”您的批处理:

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(130), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.now())
    ...

    def __repr__(self):
        return f"Post('{self.title}', '{self.date_posted}')"

因此,最后每个批次将只包含一份文档。