use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.
the class QueryDataSnippets method prepareExamples.
/**
* Creates cities collection and add sample documents to test queries.
*
* @return collection reference
*/
void prepareExamples() throws Exception {
// [START fs_query_create_examples]
CollectionReference cities = db.collection("cities");
List<ApiFuture<WriteResult>> futures = new ArrayList<>();
futures.add(cities.document("SF").set(new City("San Francisco", "CA", "USA", false, 860000L)));
futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L)));
futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L)));
futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L)));
futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L)));
// (optional) block on documents successfully added
ApiFutures.allAsList(futures).get();
// [END fs_query_create_examples]
}
use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.
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]
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.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method deleteFields.
/**
* Delete specific fields when updating a document.
*/
void deleteFields() throws Exception {
City city = new City("Beijing");
city.setCapital(true);
db.collection("cities").document("BJ").set(city).get();
// [START fs_delete_fields]
DocumentReference docRef = db.collection("cities").document("BJ");
Map<String, Object> updates = new HashMap<>();
updates.put("capital", FieldValue.delete());
// Update and delete the "capital" field in the document
ApiFuture<WriteResult> writeResult = docRef.update(updates);
System.out.println("Update time : " + writeResult.get());
// [END fs_delete_fields]
}
use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method runSimpleTransaction.
// [END fs_delete_collection]
/**
* Run a simple transaction to perform a field value increment.
*
* @return transaction future
*/
ApiFuture<Void> runSimpleTransaction() throws Exception {
// [START fs_run_simple_transaction]
// Initialize doc
final DocumentReference docRef = db.collection("cities").document("SF");
City city = new City("SF");
city.setCountry("USA");
city.setPopulation(860000L);
docRef.set(city).get();
// run an asynchronous transaction
ApiFuture<Void> transaction = db.runTransaction(new Transaction.Function<Void>() {
@Override
public Void updateCallback(Transaction transaction) throws Exception {
// retrieve document and increment population field
DocumentSnapshot snapshot = transaction.get(docRef).get();
long oldPopulation = snapshot.getLong("population");
transaction.update(docRef, "population", oldPopulation + 1);
return null;
}
});
// [END fs_run_simple_transaction]
return transaction;
}
use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method updateUsingMap.
/**
* Partially update fields of a document using a map (field => value).
*/
void updateUsingMap() throws Exception {
db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
// [START fs_update_doc_map]
// update multiple fields using a map
DocumentReference docRef = db.collection("cities").document("DC");
Map<String, Object> updates = new HashMap<>();
updates.put("name", "Washington D.C.");
updates.put("country", "USA");
updates.put("capital", true);
// asynchronously update doc
ApiFuture<WriteResult> writeResult = docRef.update(updates);
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END fs_update_doc_map]
}
Aggregations