use of com.google.cloud.firestore.DocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.
the class RetrieveDataSnippetsIT method testRetrieveAllDocuments.
@Test
public void testRetrieveAllDocuments() throws Exception {
List<QueryDocumentSnapshot> docs = retrieveDataSnippets.getAllDocuments();
assertEquals(docs.size(), 5);
Set<String> docIds = new HashSet<>();
for (DocumentSnapshot doc : docs) {
docIds.add(doc.getId());
}
assertTrue(docIds.contains("SF") && docIds.contains("LA") && docIds.contains("DC") && docIds.contains("TOK") && docIds.contains("BJ"));
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.
the class QueryDataSnippets method createAQueryAlternate.
/**
* Creates a sample query.
*
* @return query
*/
Query createAQueryAlternate() throws Exception {
// [START fs_create_query_country]
// Create a reference to the cities collection
CollectionReference cities = db.collection("cities");
// Create a query against the collection.
Query query = cities.whereEqualTo("state", "CA");
// retrieve query results asynchronously using query.get()
ApiFuture<QuerySnapshot> querySnapshot = query.get();
for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
System.out.println(document.getId());
}
// [END fs_create_query_country]
return query;
}
use of com.google.cloud.firestore.DocumentSnapshot in project java-docs-samples by GoogleCloudPlatform.
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]
// 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-docs-samples by GoogleCloudPlatform.
the class BaseIntegrationTest method deleteAllDocuments.
protected static void deleteAllDocuments(Firestore db) 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-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method returnInfoFromTransaction.
/**
* Return information from a conditional transaction.
*
* @param population : set initial population.
*/
String returnInfoFromTransaction(long population) throws Exception {
Map<String, Object> map = new HashMap<>();
map.put("population", population);
// Block until transaction is complete is using transaction.get()
db.collection("cities").document("SF").set(map).get();
// [START fs_return_info_transaction]
final DocumentReference docRef = db.collection("cities").document("SF");
ApiFuture<String> transaction = db.runTransaction(new Transaction.Function<String>() {
@Override
public String updateCallback(Transaction transaction) throws Exception {
DocumentSnapshot snapshot = transaction.get(docRef).get();
Long newPopulation = snapshot.getLong("population") + 1;
// conditionally update based on current population
if (newPopulation <= 1000000L) {
transaction.update(docRef, "population", newPopulation);
return "Population increased to " + newPopulation;
} else {
throw new Exception("Sorry! Population is too big.");
}
}
});
// Print information retrieved from transaction
System.out.println(transaction.get());
// [END fs_return_info_transaction]
return transaction.get();
}
Aggregations