Javascript Firestore更新数组字段中的单个项

Javascript Firestore更新数组字段中的单个项,javascript,arrays,firebase,object,google-cloud-firestore,Javascript,Arrays,Firebase,Object,Google Cloud Firestore,我在Firebase Firestore中有一个类似于以下内容的文档。这里的要点是,我有一个名为items的数组,其中包含对象: { name: 'Foo', items: [ { name: 'Bar', meta: { image: 'xyz.png', description: 'hello world' } }, { name: 'Rawr', meta: { imag

我在Firebase Firestore中有一个类似于以下内容的文档。这里的要点是,我有一个名为
items
的数组,其中包含对象:

{
 name: 'Foo',
 items: [
   {
     name: 'Bar',
     meta: {
        image: 'xyz.png',
        description: 'hello world'
     }
   },
   {
     name: 'Rawr',
     meta: {
        image: 'abc.png',
        description: 'hello tom'
     }
   }
 ]
}
我试图更新项目数组中元对象下的一个字段。例如项目[0].meta.description从hello world到hello bar

最初,我尝试这样做:

  const key = `items.${this.state.index}.meta.description`
  const property = `hello bar`;

  this.design.update({
    [key]: property
  })
  .then(() => {
    console.log("done")
  })
  .catch(function(error) {
    message.error(error.message);
  });
但这似乎不起作用,因为它删除了我想要修改的项索引中的所有内容,只是将描述保留在元对象下

我现在尝试下面的方法,基本上用新数据重写整个元对象

  const key = `items.${this.state.index}.meta`
  const property = e.target.value;
  let meta = this.state.meta;
  meta[e.target.id] = property;

  this.design.update({
    [key]: meta
  })
  .then(() => {
    this.setState({
    [key]: meta
    })
  })
  .catch(function(error) {
    message.error(error.message);
  });
但不幸的是,这似乎将我的整个items数组变成了一个如下所示的对象:

{
 name: 'Foo',
 items: {

   0: {
     name: 'Bar',
     meta: {
        image: 'xyz.png',
        description: 'hello world'
     }
   },
   1: {
     name: 'Rawr',
     meta: {
        image: 'abc.png',
        description: 'hello tom'
     }
   }
 }
}

你知道如何更新我想要的内容吗?

Firestore无法更新索引数组中的现有元素。中介绍了更新的唯一数组选项-您可以向数组中添加新元素(“arrayUnion”)或删除元素(“arrayRemove”)


或者,您可以从文档中读取整个数组,在内存中对其进行修改,然后完全更新修改后的数组字段。

“或者,您可以从文档中读取整个数组,在内存中对其进行修改,然后完全更新修改后的数组字段。”如果在服务器上我的抓取-修改-更新之间,其他用户及时更新了这个数组的其他项,它会不会更新我最初用我刚做的更改获取的旧数组?然后使用事务以原子方式读写文档。