如何更新Cloud Firestore中的多个文档?

问题描述 投票:0回答:1

我已经编写了一个API,我已在其中实现了更新单个文档的功能,但是我想更新多个文档。我该如何解决这个问题?

app.post('/updateO', async(req,res)=> {       
  const idRegex =  /0^(?!..?$)(?!.*__.*__)([^/]{1,1500})$/;

  if (req.body.hasOwnProperty('officeId') && req.body.names ==='office')
  {
    console.log('in the program')
    const officeID = req.body.officeId;
    console.log(officeID)
    console.log(typeof('74O37yda8rX0vbY0EprZ'))    
    console.log(String(officeID).match(idRegex))

    if (!idRegex.test(officeID))
    { 
      const data = {requestBody:req.body}
      const officeDb = await db.collection('Offices').doc(officeID).set(data.requestBody,{merge:false})

      res.json({id:officeDb.id})
    }
  }  
  else if (req.body.hasOwnProperty('recipientId') && req.body.names === 'recipient')
  {
    const recipientId = req.body.recipientId
    if (idRegex.test(recipientId))
    {
      const recipientData = await db.collection('Offices').doc(officeID).collection('Recipients').doc(recipientId).set(req.body.name,{merge})
    }
  }
  else {
    res.status(406).status('Not acceptable')
  }        
})
javascript node.js express google-cloud-firestore
1个回答
0
投票

您可以批量处理,但请注意,如果有一个子集合,那么它将无法正常工作,请阅读更多示例here

© www.soinside.com 2019 - 2024. All rights reserved.