在Firestore Firebase中插入和合并特定字段

时间:2018-08-31 05:32:59

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

我的收藏夹中有这种格式的文档

name: xyz,
email: xyz@email.com,
age: 30,
address: {
    street_no: {
        complete_address: somedata,
        pincode: somepin
    },
    city:somecity,
    state:somestate,
    landmark:nearby
}

在此文档中,我试图将complete_address与先前的记录合并。为此,我正在尝试

const database = firebase.firestore();
var dataRef = database.collection('collection');
var query = dataRef.doc(key+"").get().then(doc=>{
    if(!doc.exists){
        res.send("doesn't exist");
    }else{
        //few checks
        if(doc.data().accessid != accessid){
            res.send("accessid doesn't match")
        }
        //here I am trying to insert and merge with the previous data
        var form_path = 'address.street_no.complete_address';
        dataRef.doc(key+"."+form_path).set({
            another_address
        }, {merge: true});
    }
}).catch(err=>{
    console.log(err)
})

但是当我执行此操作时,只需在集合中添加另一个文档,然后再添加此路径key.address.street_no.complete_address

我该怎么做才能只与先前的complete_address插入并合并?

.中有/而不是form_path,因为从这个link那里得到的想法很少

2 个答案:

答案 0 :(得分:1)

我相信您的问题将在接下来的两行之内

var form_path = 'address.street_no.complete_address';

接下来,您正在使用dataRef.doc(key+"."+form_path)

这意味着要设置的唯一文档是

/addressCollection/key.{addressCollectionId}

addressCollectionIdaddress.street_no.complete_address

相反,您要做的是像这样使用点表示法访问文档中的属性。

address: {
    street_no: {
        complete_address

示例。

someDocument.update({
    "address.street_no.complete_address": "some_data"
});

请注意,“ some_data”将替换当前存储的所有数据。您将要进行一次读取并合并数据。例如。

  const anotherAddress = { address: "123 Fake Street" };

  const document = firebase
    .firestore()
    .collection("addressCollection")
    .doc("someAddressId");

  document
    .get()
    .then(snap => {
      const data = snap.data();
      const completeAddress = data.address.street_no.complete_address };

      // We're using the spread operator here to combine the current completeAddress with anotherAddress
      return { completeAddress, ...anotherAddress };
    })
    .then(newCompleteAddress =>
      document.update({
        "address.street_no.complete_address": newCompleteAddress
      })
    );

答案 1 :(得分:0)

我得到了这个工作。

因此,我弄清楚了我之前尝试做的事情将在集合中创建一个包含有关该数据的文档。因此,我开始将所有内容都视为对象,并将对象数据传递给set()方法。

const database = firebase.firestore();
var dataRef = database.collection('collection');
var query = dataRef.doc(key+"").get().then(doc=>{
    if(!doc.exists){
        res.send("doesn't exist");
    }else{
        //few checks
        if(doc.data().accessid != accessid){
            res.send("accessid doesn't match")
        }
        //here I am trying to insert and merge with the previous data
        var mergeData = {
            address : {
            }
        }
        var new_address = {
            key: "address_data"
        }
        mergeData.address[street_no] = {complete_address : address}
        if(dataRef.doc(key+"").set(mergeData, {merge: true})){
            res.send("done")
        }else{
            res.send("failed")
        }
    }
}).catch(err=>{
    console.log(err)
})