Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ Firestore getDb() {
void addDocument(String docName) throws Exception {
switch (docName) {
case "alovelace": {
// [START fs_add_data_1]
// [START firestore_setup_dataset_pt1]
DocumentReference docRef = db.collection("users").document("alovelace");
// Add document data with id "alovelace" using a hashmap
Expand All @@ -98,11 +97,9 @@ void addDocument(String docName) throws Exception {
// result.get() blocks on response
System.out.println("Update time : " + result.get().getUpdateTime());
// [END firestore_setup_dataset_pt1]
// [END fs_add_data_1]
break;
}
case "aturing": {
// [START fs_add_data_2]
// [START firestore_setup_dataset_pt2]
DocumentReference docRef = db.collection("users").document("aturing");
// Add document data with an additional field ("middle")
Expand All @@ -115,7 +112,6 @@ void addDocument(String docName) throws Exception {
ApiFuture<WriteResult> result = docRef.set(data);
System.out.println("Update time : " + result.get().getUpdateTime());
// [END firestore_setup_dataset_pt2]
// [END fs_add_data_2]
break;
}
case "cbabbage": {
Expand Down Expand Up @@ -156,6 +152,7 @@ void runQuery() throws Exception {
}

void retrieveAllDocuments() throws Exception {
// [START firestore_setup_dataset_read]
// [START fs_get_all]
// asynchronously retrieve all users
ApiFuture<QuerySnapshot> query = db.collection("users").get();
Expand All @@ -173,6 +170,7 @@ void retrieveAllDocuments() throws Exception {
System.out.println("Born: " + document.getLong("born"));
}
// [END fs_get_all]
// [END firestore_setup_dataset_read]
}

void run() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class ManageDataSnippets {
* @return document data
*/
Map<String, Object> addSimpleDocumentAsMap() throws Exception {
// [START fs_add_doc_as_map]
// [START firestore_data_set_from_map]
// Create a Map to store the data we want to set
Map<String, Object> docData = new HashMap<>();
Expand All @@ -65,7 +64,6 @@ Map<String, Object> addSimpleDocumentAsMap() throws Exception {
// future.get() blocks on response
System.out.println("Update time : " + future.get().getUpdateTime());
// [END firestore_data_set_from_map]
// [END fs_add_doc_as_map]
return docData;
}

Expand All @@ -75,7 +73,6 @@ Map<String, Object> addSimpleDocumentAsMap() throws Exception {
* @return document data
*/
Map<String, Object> addDocumentWithDifferentDataTypes() throws Exception {
// [START fs_add_doc_data_types]
// [START firestore_data_set_from_map_nested]
Map<String, Object> docData = new HashMap<>();
docData.put("stringExample", "Hello, World");
Expand All @@ -96,7 +93,6 @@ Map<String, Object> addDocumentWithDifferentDataTypes() throws Exception {
ApiFuture<WriteResult> future = db.collection("data").document("one").set(docData);
System.out.println("Update time : " + future.get().getUpdateTime());
// [END firestore_data_set_from_map_nested]
// [END fs_add_doc_data_types]

return docData;
}
Expand All @@ -107,26 +103,22 @@ Map<String, Object> addDocumentWithDifferentDataTypes() throws Exception {
* @return entity added
*/
City addSimpleDocumentAsEntity() throws Exception {
// [START fs_add_simple_doc_as_entity]
// [START firestore_data_set_from_custom_type]
City city =
new City("Los Angeles", "CA", "USA", false, 3900000L, Arrays.asList("west_coast", "socal"));
ApiFuture<WriteResult> future = db.collection("cities").document("LA").set(city);
// block on response if required
System.out.println("Update time : " + future.get().getUpdateTime());
// [END firestore_data_set_from_custom_type]
// [END fs_add_simple_doc_as_entity]

return city;
}

/** set() providing a document ID. */
void setRequiresId(Map<String, String> data) {
// [START fs_set_requires_id]
// [START firestore_data_set_id_specified]
db.collection("cities").document("new-city-id").set(data);
// [END firestore_data_set_id_specified]
// [END fs_set_requires_id]
}

/**
Expand All @@ -136,7 +128,6 @@ void setRequiresId(Map<String, String> data) {
* @return auto generated id
*/
String addDocumentDataWithAutoGeneratedId() throws Exception {
// [START fs_add_doc_data_with_auto_id]
// [START firestore_data_set_id_random_collection]
// Add document data with auto-generated id.
Map<String, Object> data = new HashMap<>();
Expand All @@ -145,7 +136,6 @@ String addDocumentDataWithAutoGeneratedId() throws Exception {
ApiFuture<DocumentReference> addedDocRef = db.collection("cities").add(data);
System.out.println("Added document with ID: " + addedDocRef.get().getId());
// [END firestore_data_set_id_random_collection]
// [END fs_add_doc_data_with_auto_id]

return addedDocRef.get().getId();
}
Expand All @@ -158,7 +148,6 @@ String addDocumentDataWithAutoGeneratedId() throws Exception {
String addDocumentDataAfterAutoGeneratingId() throws Exception {
City data = new City();

// [START fs_add_doc_data_after_auto_id]
// [START firestore_data_set_id_random_document_ref]
// Add document data after generating an id.
DocumentReference addedDocRef = db.collection("cities").document();
Expand All @@ -167,7 +156,6 @@ String addDocumentDataAfterAutoGeneratingId() throws Exception {
// later...
ApiFuture<WriteResult> writeResult = addedDocRef.set(data);
// [END firestore_data_set_id_random_document_ref]
// [END fs_add_doc_data_after_auto_id]

// writeResult.get() blocks on operation
System.out.println("Update time : " + writeResult.get().getUpdateTime());
Expand All @@ -177,7 +165,6 @@ String addDocumentDataAfterAutoGeneratingId() throws Exception {
/** Partially update a document using the .update(field1, value1..) method. */
void updateSimpleDocument() throws Exception {
db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
// [START fs_update_doc]
// [START firestore_data_set_field]
// Update an existing document
DocumentReference docRef = db.collection("cities").document("DC");
Expand All @@ -189,7 +176,6 @@ void updateSimpleDocument() throws Exception {
WriteResult result = future.get();
System.out.println("Write result: " + result);
// [END firestore_data_set_field]
// [END fs_update_doc]
}

/** Partially update fields of a document using a map (field => value). */
Expand All @@ -213,7 +199,6 @@ void updateUsingMap() throws Exception {

/** Partially update fields of a document using a map (field => value). */
void updateAndCreateIfMissing() throws Exception {
// [START fs_update_create_if_missing]
// [START firestore_data_set_doc_upsert]
// asynchronously update doc, create the document if missing
Map<String, Object> update = new HashMap<>();
Expand All @@ -224,13 +209,11 @@ void updateAndCreateIfMissing() throws Exception {
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END firestore_data_set_doc_upsert]
// [END fs_update_create_if_missing]
}

/** Partial update nested fields of a document. */
void updateNestedFields() throws Exception {
// CHECKSTYLE OFF: VariableDeclarationUsageDistance
// [START fs_update_nested_fields]
// [START firestore_data_set_nested_fields]
// Create an initial document to update
DocumentReference frankDocRef = db.collection("users").document("frank");
Expand Down Expand Up @@ -258,27 +241,23 @@ void updateNestedFields() throws Exception {
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END firestore_data_set_nested_fields]
// [END fs_update_nested_fields]
// CHECKSTYLE ON: VariableDeclarationUsageDistance
}

/** Update document with server timestamp. */
void updateServerTimestamp() throws Exception {
db.collection("objects").document("some-id").set(new HashMap<String, Object>()).get();

// [START fs_update_server_timestamp]
// [START firestore_data_set_server_timestamp]
DocumentReference docRef = db.collection("objects").document("some-id");
// Update the timestamp field with the value from the server
ApiFuture<WriteResult> writeResult = docRef.update("timestamp", FieldValue.serverTimestamp());
System.out.println("Update time : " + writeResult.get());
// [END firestore_data_set_server_timestamp]
// [END fs_update_server_timestamp]
}

/** Update array fields in a document. * */
void updateDocumentArray() throws Exception {
// [START fs_update_document_array]
// [START firestore_data_set_array_operations]
DocumentReference washingtonRef = db.collection("cities").document("DC");

Expand All @@ -292,7 +271,6 @@ void updateDocumentArray() throws Exception {
washingtonRef.update("regions", FieldValue.arrayRemove("east_coast"));
System.out.println("Update time : " + arrayRm.get());
// [END firestore_data_set_array_operations]
// [END fs_update_document_array]
}

/** Delete specific fields when updating a document. */
Expand All @@ -301,7 +279,6 @@ void deleteFields() throws Exception {
city.setCapital(true);
db.collection("cities").document("BJ").set(city).get();

// [START fs_delete_fields]
// [START firestore_data_delete_field]
DocumentReference docRef = db.collection("cities").document("BJ");
Map<String, Object> updates = new HashMap<>();
Expand All @@ -310,23 +287,19 @@ void deleteFields() throws Exception {
ApiFuture<WriteResult> writeResult = docRef.update(updates);
System.out.println("Update time : " + writeResult.get());
// [END firestore_data_delete_field]
// [END fs_delete_fields]
}

/** Delete a document in a collection. */
void deleteDocument() throws Exception {
db.collection("cities").document("DC").set(new City("Washington, D.C.")).get();
// [START fs_delete_doc]
// [START firestore_data_delete_doc]
// asynchronously delete a document
ApiFuture<WriteResult> writeResult = db.collection("cities").document("DC").delete();
// ...
System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END firestore_data_delete_doc]
// [END fs_delete_doc]
}

// [START fs_delete_collection]
// [START firestore_data_delete_collection]
/**
* Delete a collection in batches to avoid out-of-memory errors. Batch size may be tuned based on
Expand All @@ -352,15 +325,13 @@ void deleteCollection(CollectionReference collection, int batchSize) {
}
}
// [END firestore_data_delete_collection]
// [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]
// [START firestore_transaction_document_update]
// Initialize doc
final DocumentReference docRef = db.collection("cities").document("SF");
Expand All @@ -381,7 +352,6 @@ ApiFuture<Void> runSimpleTransaction() throws Exception {
});
// block on transaction operation using transaction.get()
// [END firestore_transaction_document_update]
// [END fs_run_simple_transaction]
return futureTransaction;
}

Expand All @@ -395,7 +365,6 @@ String returnInfoFromTransaction(long population) throws Exception {
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]
// [START firestore_transaction_document_update_conditional]
final DocumentReference docRef = db.collection("cities").document("SF");
ApiFuture<String> futureTransaction =
Expand All @@ -414,7 +383,6 @@ String returnInfoFromTransaction(long population) throws Exception {
// Print information retrieved from transaction
System.out.println(futureTransaction.get());
// [END firestore_transaction_document_update_conditional]
// [END fs_return_info_transaction]
return futureTransaction.get();
}

Expand All @@ -423,7 +391,6 @@ void writeBatch() throws Exception {
db.collection("cities").document("SF").set(new City()).get();
db.collection("cities").document("LA").set(new City()).get();

// [START fs_write_batch]
// [START firestore_data_batch_writes]
// Get a new write batch
WriteBatch batch = db.batch();
Expand All @@ -448,23 +415,20 @@ void writeBatch() throws Exception {
System.out.println("Update time : " + result.getUpdateTime());
}
// [END firestore_data_batch_writes]
// [END fs_write_batch]
}

public void updateDocumentIncrement() throws ExecutionException, InterruptedException {
final City city = new City();
city.setPopulation(100L);
db.collection("cities").document("DC").set(city).get();

// [START fs_update_document_increment]
// [START firestore_data_set_numeric_increment]
DocumentReference washingtonRef = db.collection("cities").document("DC");

// Atomically increment the population of the city by 50.
final ApiFuture<WriteResult> updateFuture =
washingtonRef.update("population", FieldValue.increment(50));
// [END firestore_data_set_numeric_increment]
// [END fs_update_document_increment]
updateFuture.get();
}

Expand Down
Loading