簡體   English   中英

將數據寫入 firebase firestore 中嵌套的 object

[英]Write data in to nested object in firebase firestore

我有一個如下所示的數據結構: 在此處輸入圖像描述

這是集合的頂層: 在此處輸入圖像描述

我想寫來增加字段數,但我做不到。 我已經嘗試了很多不同的方法,我不想 go 通過所有這些方法。 我最接近的是通過:

const pageRef = admin.firestore().collection("pages").doc(image.page);

pageRef.set(
  {
    [`images.${image.position}.count`]: admin.firestore.FieldValue.increment(
      1
    ),
  },
  { merge: true }
);

但這給我留下了: 在此處輸入圖像描述

請幫忙。 更改頁面結構是一種選擇。

這就是我試圖復制的內容: Update fields in nested objects in firestore documents?

問題在於如何使用點符號。

在您分享的帖子中,他們使用的示例是:

 var setAda = dbFirestore.collection('users').doc('alovelace').update({ "first.test": "12345" });

將此應用於您的代碼和 model 將是:

const pageRef = admin.firestore().collection("pages").doc(image.page);

pageRef.set(
  {
    `images[${image.position}].count`: admin.firestore.FieldValue.increment(
      1
    ),
  },
  { merge: true }
);

這將影響 Array Images 中的元素,元素image.position其值計數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM