Deleting element from a document

What is the best way to achieve this on a document?
I am trying to delete “experience2”
I am using Python.
Many thanks!!

Document at start:

{
  "ref": Ref(Collection("users"), "340265987143106639"),
  "ts": 1660761862170000,
  "data": {
    "account": {
      "usertype": "Engineering Talent",
    },
    "profile": {
      "photo": "photo_url",
      "phone": "12312312",
    },
    "experience": {
      "experience1": {
        "title": "Staff Engineer",
        "type": "Full-Time",
      },
      "experience2": {
        "title": "Staff Engineer",
        "type": "Full-Time",
      },
    },
    "date": Time("2022-08-17T18:43:02.542151Z")
  }
}

Document after change:

{
  "ref": Ref(Collection("users"), "340265987143106639"),
  "ts": 1660761862170000,
  "data": {
    "account": {
      "usertype": "Engineering Talent",
    },
    "profile": {
      "photo": "photo_url",
      "phone": "12312312",
    },
    "experience": {
      "experience1": {
        "title": "Staff Engineer",
        "type": "Full-Time",
      }
    },
    "date": Time("2022-08-17T18:43:02.542151Z")
  }
}

Fauna doesn’t store null values. So if you Update a document and set a field to null, that field is removed from the document.

Thanks for providing the entire document that you are using. It helps me to demonstrate the specific change that you want to make. I’ve included all of the steps that I took to reproduce the document and then adjust it.

Collection creation:

> CreateCollection({ name: "users" })
{
  ref: Collection("users"),
  ts: 1660763167960000,
  history_days: 30,
  name: 'users'
}

Document creation:

> Create(
  Ref(Collection("users"), "340265987143106639"),
  {
    data: {
      "account": {
        "usertype": "Engineering Talent",
      },
      "profile": {
        "photo": "photo_url",
        "phone": "12312312",
      },
      "experience": {
        "experience1": {
          "title": "Staff Engineer",
          "type": "Full-Time",
        },
        "experience2": {
          "title": "Staff Engineer",
          "type": "Full-Time",
        },
      },
      "date": Time("2022-08-17T18:43:02.542151Z")
    }
  }
)
{
  ref: Ref(Collection("users"), "340265987143106639"),
  ts: 1660763293580000,
  data: {
    account: { usertype: 'Engineering Talent' },
    profile: { photo: 'photo_url', phone: '12312312' },
    experience: {
      experience1: { title: 'Staff Engineer', type: 'Full-Time' },
      experience2: { title: 'Staff Engineer', type: 'Full-Time' }
    },
    date: Time("2022-08-17T18:43:02.542151Z")
  }
}

Document modification:

> Update(
  Ref(Collection("users"), "340265987143106639"),
  {
    data: {
      experience: {
        experience2: null
      }
    }
  }
)
{
  ref: Ref(Collection("users"), "340265987143106639"),
  ts: 1660763362700000,
  data: {
    account: { usertype: 'Engineering Talent' },
    profile: { photo: 'photo_url', phone: '12312312' },
    experience: { experience1: { title: 'Staff Engineer', type: 'Full-Time' } },
    date: Time("2022-08-17T18:43:02.542151Z")
  }
}
1 Like

Thank you! Works like a charm.

Actually,

I’m wondering how to set the null field when using the following method for calling the update function:

client.query(
                q.update(
                    q.ref(q.collection("users"), session["user"]["id"]),
                    {
                        "data": {
                            "experience": {
                                experience2: "null"
                                },
                            }
                        },
                    )
                )

The previous method wipes the contents inside “experience2”, however, the element itself does not get erased. Thanks!

I think the answer is: None

You are correct: when using the Python driver, use None when you want to indicate null. When you specified "null", that’s a string, so it’s not surprising that it remained in the document after the update.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.