Updating a field contains period (.) is not working as expected.
In docs, nested fields can be updated by providing dot-seperated filed path strings or by providing FieldPath objects.
So if I have a field and it's key is "com.example.android"
how I can update this field (from Android)?
In my scenario I've to set the document if it's not exists otherwise update the document. So first set is creating filed contains periods like above and then trying update same field it's creating new field with nested fields because it contains periods.
db.collection(id).document(uid).update(pkg, score)
What you want to do is possible:
This is happening because the
.
(dot) symbol is used as a separator between objects that exist within Cloud Firestore documents. That's why you have this behaviour. To solve this, please avoid using the.
symbol inside the key of the object. So in order to solve this, you need to change the way you are setting that key. So please change the following key:with
And you'll be able to update your property without any issue. This can be done in a very simple way, by encoding the key when you are adding data to the database. So please use the following method to encode the key:
And this method, to decode the key:
Edit:
Acording to your comment, if you have a key that looks like this:
This case can be solved in a very simple way, by encoding/decoding the key twice. First econde the
_
to@
, second encode.
to_
. When decoding, first decode_
to.
and second, decode@
to_
. Let's take a very simple example:Here are the corresponding methods:
The output will be:
But note, this is only an example, you can encode/decode this key according to the use-case of your app. This a very common practice when it comes to encoding/decoding strings.
Key should not contains periods (
.
), since it's conflicting with nested fields. An ideal solution is don't make keys are dynamic, those can not be determined. Then you have full control over how the keys should be.It's fixed now. It's not happening like that.