如何在Google Cloud Firestore中删除子集合项

时间:2019-04-18 12:25:49

标签: typescript google-cloud-firestore

我正在与Google Cloud Firestore一起进行角度项目。然后我有一个问题。如何删除子集合项。例如,我想在下面删除地址。我将ng2-smart-table用作表格。我已将其添加到manufacture组件中。

manufacture.service.ts

import { Injectable } from '@angular/core';
import { Manufacture } from './manufacture.model';
import { AngularFirestore } from '@angular/fire/firestore';
import { from } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class ManufactureService {
    manufacture: Manufacture;

    constructor(private firestore: AngularFirestore) {}

    addManufacture(object) {
        return from(
        this.firestore
            .collection('Manufacture')
            .doc('manufacture')
            .set(object)
        );
    }

    getManufacture() {
        return this.firestore
        .collection('Manufacture')
        .doc('manufacture')
        .snapshotChanges();
    }

    deleteManufacture() {
        this.firestore
        .collection('Manufacture')
        .doc('manufacture')
        .delete();      
    }

    show() {
        return this.firestore
        .collection('Manufacture')
        .doc('manufacture')
        .collection('manufact')
        .doc('0')
        .snapshotChanges();
    }
}

manufacture.component.ts

import { Component, OnInit } from '@angular/core';
import { ManufactureService } from './manufacture.service';
import { Manufacture } from './manufacture.model';

@Component({
selector: 'ngx-manufacture',
styles: [],
template: `
    <ng2-smart-table
    (createConfirm)="addData($event)"
    (deleteConfirm)="deleteData($event)"
    [settings]="settings"
    [source]="manu"
    >
    </ng2-smart-table>
`
})
export class ManufactureComponent implements OnInit {
    manu: Manufacture[] = [];

    constructor(private service: ManufactureService) {}

    ngOnInit() {
        this.service.getManufacture().subscribe(arr => {
        let manu_list = arr.payload.get('manufact');
        if (manu_list) {
            this.manu = manu_list;
        }
        });
    }

    settings = {
        add: {
        addButtonContent: '<i class="nb-plus"></i>',
        createButtonContent: '<i class="nb-checkmark"></i>',
        cancelButtonContent: '<i class="nb-close"></i>',
        confirmCreate: true
        },
        edit: {
        editButtonContent: '<i class="nb-edit"></i>',
        saveButtonContent: '<i class="nb-checkmark"></i>',
        cancelButtonContent: '<i class="nb-close"></i>'
        },
        delete: {
        deleteButtonContent: '<i class="nb-trash"></i>',
        confirmDelete: true
        },
        columns: {
        shopname: {
            title: 'Shop Name'
        },
        ownername: {
            title: 'Owner Name'
        },
        nic: {
            title: 'NIC'
        },
        contactno: {
            title: 'ContactNo'
        },
        address: {
            title: 'Address'
        },
        email: {
            title: 'Email'
        }
        }
    };

    addData(data) {
        alert(data.newData.email);
        this.manu.push(data.newData);
        console.log(this.manu);
        this.service.addManufacture({ manufact: this.manu }).subscribe(next => {
        data.confirm.reject();
        });
    }

    deleteData(data) {
        alert(data.emial);
        this.service.deleteManufacture();
    }
}

enter image description here

然后我该怎么做。一旦我尝试通过以下代码。但不是成功

deleteManufacture() {
     this.firestore
    .collection('Manufacture')
    .doc('manufacture')
    .delete();      
}

任何人都可以帮助我

1 个答案:

答案 0 :(得分:2)

您应该使用“点符号”来更新对象。

我相信会是这样:

db.collection('Manufacture').doc('manufacture').update({
  "0.address": firebase.firestore.FieldValue.delete(),
});

FieldValue是一个哨兵值,即,它用于表示某些情况,在这种情况下,我们要删除字段“ 0.address”。

如文档中所指出,它是“在使用set()或update()编写文档字段时可以使用的前哨值”。因此,您可以使用它来指示您打算在setupdate操作中对该字段执行的操作。更具体地说,delete标记了要删除的字段。还有其他诸如incrementserverTimestamp之类的东西。

Dot-operator google-cloud-firestore

FieldValue