【发布时间】:2019-02-27 21:35:56
【问题描述】:
如何在 Firestore 中获取文档的 ID?
final String PostKey = db.collection("Anuncio").document().getId();
我正在尝试这种方式,但它返回一个新的 id。如何获取已经退出的文档的de id?
【问题讨论】:
标签: java android google-cloud-firestore
如何在 Firestore 中获取文档的 ID?
final String PostKey = db.collection("Anuncio").document().getId();
我正在尝试这种方式,但它返回一个新的 id。如何获取已经退出的文档的de id?
【问题讨论】:
标签: java android google-cloud-firestore
如果您事先不知道文档 ID,可以retrieve all the documents in a collection 并打印出 ID:
db.collection("Anuncio")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
如果您对文档的子集感兴趣,可以add a query clause 过滤文档:
db.collection("Anuncio")
.whereEqualTo("some-field", "some-value")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
【讨论】: