use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class RetrieveDataSnippets method getQueryResults.
/**
* Return multiple documents from a collection based on a query.
*
* @return list of documents of capital cities.
*/
public List<QueryDocumentSnapshot> getQueryResults() throws Exception {
// [START fs_get_multiple_docs]
// [START firestore_data_query]
// asynchronously retrieve multiple documents
ApiFuture<QuerySnapshot> future = db.collection("cities").whereEqualTo("capital", true).get();
// future.get() blocks on response
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (DocumentSnapshot document : documents) {
System.out.println(document.getId() + " => " + document.toObject(City.class));
}
// [END fs_get_multiple_docs]
return documents;
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class RetrieveDataSnippets method getDocumentAsEntity.
/**
* Retrieves document in collection as a custom object.
*
* @return document data as City object
*/
public City getDocumentAsEntity() throws Exception {
// [START fs_get_doc_as_entity]
// [START firestore_data_get_as_custom_type]
DocumentReference docRef = db.collection("cities").document("BJ");
// asynchronously retrieve the document
ApiFuture<DocumentSnapshot> future = docRef.get();
// block on response
DocumentSnapshot document = future.get();
City city = null;
if (document.exists()) {
// convert document to POJO
city = document.toObject(City.class);
System.out.println(city);
} else {
System.out.println("No such document!");
}
// [END fs_get_doc_as_entity]
return city;
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class RetrieveDataSnippetsIT method deleteAllDocuments.
private static void deleteAllDocuments() throws Exception {
ApiFuture<QuerySnapshot> future = db.collection("cities").get();
QuerySnapshot querySnapshot = future.get();
for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
// block on delete operation
db.collection("cities").document(doc.getId()).delete().get();
}
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class QuickstartIT method deleteAllDocuments.
private void deleteAllDocuments() throws Exception {
ApiFuture<QuerySnapshot> future = db.collection("users").get();
QuerySnapshot querySnapshot = future.get();
for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
// block on delete operation
db.document("users/" + doc.getId()).delete().get();
}
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.
the class ManageDataSnippetsIT method testWriteBatchIsSuccessful.
@Test
public void testWriteBatchIsSuccessful() throws Exception {
manageDataSnippets.writeBatch();
CollectionReference collection = db.collection("cities");
ApiFuture<DocumentSnapshot> document = collection.document("NYC").get();
assertTrue(document.get().exists());
DocumentReference documentReference = collection.document("SF");
Map<String, Object> data = getDocumentDataAsMap(documentReference);
assertTrue(data.containsKey("population"));
document = collection.document("LA").get();
assertFalse(document.get().exists());
}
Aggregations