Setting a field to undefined using the Add Documents endpoint

I can use the Add Documents REST endpoint to add a new field to an existing document (by specifying an existing _id).

Is there any way to remove a field using the Add Document endpoint? Going by the overall documentation, that should be possible by setting the field to Undefined, but I don’t know how to encode that in Json. I tried setting it to null, but that left a null value at the field value, as opposed to deleting the field from the document (which makes sense).

As a workaround, I could use the PATCH endpoint, but I’d prefer to use the Add Document endpoint if possible (and anyway I’m curious if it’s possible at all).

Hello Moritz!

Interesting dilemma. I recommend using the rockset’s undefined type in your Add Document call. In your data block, set the field value that you wish to delete to the following:

"field_name": { "__rockset_type": "undefined" }

The resulting field will be empty - not null. Let me know if this works for you!

That sounds reasonable, but it doesn’t seem to work:

I’M POSTing

{
    "_id": <key>,
    <field>: {
      "__rockset_type": "undefined"
    },
    "_seq_no": <seqno>
}

But a subsequent SELECT * from <collection> WHERE _id=<key> returns

 {
    "_id": <key>,
    "_event_time": "2024-01-10T12:46:29.278795Z",
    "_meta": null,
    <field>: {},
    "_seq_no": 117
  }

Ie. the field is present and set to an empty object. I would have expected the field not to be present at all, i.e. the query should only return _id, _event_time, _meta and _seq_no.

Thank you for reporting back! I’ll investigate why an empty object is returned instead of an undefined field. In the meantime, another method is to use _op and apply a REPSERT operation. In your example, this would look like:

{
    "_id": <key>,
    "_op": "REPSERT",
    "_seq_no": <seqno>
}

Purposefully leave out the field you wish to delete, but include the rest of the fields. For more information about the _op field, check out the documentation on Special Fields.