Cloud Firestore生成的文档ID是客户端生成的,完全随机,不依赖于生成它们的集合。
如果你深入研究一下(开源)SDK,你可以亲眼看到这一点。例如,在Android SDK中,以下是
source for
CollectionReference.add()
:
final DocumentReference ref = document();
return ref.set(data)
这就把ID的生成留给了
document
method
:
public DocumentReference document() {
return document(Util.autoId());
}
哪些代表
Util.autoId()
:
private static final int AUTO_ID_LENGTH = 20;
private static final String AUTO_ID_ALPHABET =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
private static final Random rand = new Random();
public static String autoId() {
StringBuilder builder = new StringBuilder();
int maxRandom = AUTO_ID_ALPHABET.length();
for (int i = 0; i < AUTO_ID_LENGTH; i++) {
builder.append(AUTO_ID_ALPHABET.charAt(rand.nextInt(maxRandom)));
}
return builder.toString();
}
如前所述:纯客户端随机性,具有足够的熵以确保全局唯一性。