Firestore - How to update a field that contains pe

2019-02-25 11:32发布

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)

enter image description here

4条回答
甜甜的少女心
2楼-- · 2019-02-25 11:51

What you want to do is possible:

FieldPath field = FieldPath.of("com.example.android");
db.collection(collection).document(id).update(field, value);
查看更多
Summer. ? 凉城
3楼-- · 2019-02-25 12:06

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:

com.example.android

with

com_example_android

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:

private String encodeKey(String key) {
    return key.replace(".", "_");
}

And this method, to decode the key:

private String decodeKey(String key) {
    return key.replace("_", ".");
}

Edit:

Acording to your comment, if you have a key that looks like this:

com.social.game_1

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:

String s = "com.social.game_1";
String s1 = encodeKeyOne(s);
String s2 = encodeKeyTwo(s1);
System.out.println(s2);
String s3 = decodeKeyOne(s2);
String s4 = decodeKeyTwo(s3);
System.out.println(s4);

Here are the corresponding methods:

private static String encodeKeyOne(String key) {
    return key.replace("_", "@");
}

private static String encodeKeyTwo(String key) {
    return key.replace(".", "_");
}

private static String decodeKeyOne(String key) {
    return key.replace("_", ".");
}

private static String decodeKeyTwo(String key) {
    return key.replace("@", "_");
}

The output will be:

com_social_game@1
com.social.game_1 //The exact same String as the initial one

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.

查看更多
欢心
4楼-- · 2019-02-25 12:09

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.

查看更多
够拽才男人
5楼-- · 2019-02-25 12:13

It's fixed now. It's not happening like that.

查看更多
登录 后发表回答