使用动态密钥从Firestore中删除字段

时间:2017-11-23 10:11:21

标签: javascript firebase google-cloud-firestore

我正在尝试从Firestore中的Document中删除单个字段 该字段的密钥保存在一个变量中,例如

var userId = "random-id-1"

在文档中,我有一个像这样结构的成员领域:

{
  members:{
    random-id-1:true,
    random-id-2:true
  }
}

我想删除random-id-1:true,但请保留random-id-2:true

如果不获取整个成员对象并编写更新的对象,这怎么可能?

我已尝试this,但收到错误:Document references must have an even number of segments

我也试过这个:

db.collection('groups').doc(this.props.groupId).set({
  members: {
    [userId]: firebase.firestore.FieldValue.delete()
  }
},{merge: true})

但是我收到错误:Function DocumentReference.update() called with invalid data. FieldValue.delete() can only appear at the top level of your update data

感谢您的帮助

2 个答案:

答案 0 :(得分:21)

我设法删除了这样的字段:

let userId = "this-is-my-user-id"
let groupId = "this-is-my-group-id"

db.collection('groups').doc(groupId).update({
  ['members.' + userId]: firebase.firestore.FieldValue.delete()
})

这是使用描述here

的点运算符方法

如果有任何替代方法,请告知我们

由于

答案 1 :(得分:1)

我必须导入FieldValue

https://firebase.google.com/docs/firestore/manage-data/delete-data#fields

   private void buttonPlayVideoDevice_Click(object sender, EventArgs e)
    {
        if (comboBoxDevices.SelectedIndex < 0)
            return;
        StopPlaying();
        Device device = (Device)comboBoxDevices.Items[comboBoxDevices.SelectedIndex];
        if (m_player.OpenCamera(device, m_vcam_filter))
        {
            buttonStopVideoDevice.Enabled = true;
            m_player.Run();
        }
      }

  public bool OpenCamera(Device i_device, IBaseFilter i_vcam_filter)
    {
        // Stop and release interfaces
        Cleanup();
        bool succeeded = true;
        IPin pin_out = null;
        IPin pin_in = null;
        int hr = 0;

        // Create an instance of FilterGraph interface
        m_graph_builder = (IGraphBuilder)new FilterGraph();

        // Add camera source filter to our graph.
        IBaseFilter filter_source = i_device.CreateDevice();
        if (0 != (hr = m_graph_builder.AddFilter(filter_source, "Source Filter")))
        {
            succeeded = false;
            goto exit;
        }

        // Add VCam Render filter to graph, the VCam Render will pass video frames to VCam
        if (0 != (hr = m_graph_builder.AddFilter(i_vcam_filter, "VCam Renderer Filter")))
        {
            succeeded = false;
            goto exit;
        }

        pin_out = DsFindPin.ByDirection(filter_source, PinDirection.Output, 0);
        pin_in = DsFindPin.ByDirection(i_vcam_filter, PinDirection.Input, 0);
        if (pin_out == null || pin_in == null)
        {
            succeeded = false;
            goto exit;
        }

        if (0 != (hr = m_graph_builder.Connect(pin_out, pin_in)))
        {
            succeeded = false;
            goto exit;
        }

        m_control = (IMediaControl)m_graph_builder;      
        exit:
          if (filter_source != null)                                                                                      Marshal.ReleaseComObject(filter_source);
        if (pin_out != null) Marshal.ReleaseComObject(pin_out);
        if (pin_in != null) Marshal.ReleaseComObject(pin_in);
        return succeeded;            
    }