我在使用Cloud Firestore生成随机唯一ID时遇到问题.以前我使用实时数据库并生成随机字符串我使用childByAutoID().key. 有没有办法为Cloud Firestore做类似的事情? 正如杰伊在评论中所说,这不是
有没有办法为Cloud Firestore做类似的事情?
正如杰伊在评论中所说,这不是重复,因为我正在尝试生成一个随机字符串,我不是想要随机文档.
如果您创建的文档没有明确的ID,我们的SDK会自动生成一个随机的文档.// Add a new document with a generated id.
var ref: DocumentReference? = nil
ref = db.collection("messages").addDocument(data: [
"sender": "<my_senders_id>",
"recipient": "<my_recipient_id>",
"message": "Hello from Google Cloud Platform & Firebase!",
"status": "unread"
]) { err in
if let err = err {
print("Error adding document: \(err)")
} else {
print("Document added with ID: \(ref!.documentID)")
}
}
