Search in sources :

Example 21 with DocumentReference

use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.

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());
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) CollectionReference(com.google.cloud.firestore.CollectionReference) DocumentReference(com.google.cloud.firestore.DocumentReference) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 22 with DocumentReference

use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method updateSimpleDocument.

/**
 * 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]
    // Update an existing document
    DocumentReference docRef = db.collection("cities").document("DC");
    // (async) Update one field
    ApiFuture<WriteResult> future = docRef.update("capital", true);
    // ...
    WriteResult result = future.get();
    System.out.println("Write result: " + result);
// [END fs_update_doc]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 23 with DocumentReference

use of com.google.cloud.firestore.DocumentReference 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();
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Transaction(com.google.cloud.firestore.Transaction) HashMap(java.util.HashMap) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 24 with DocumentReference

use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method addDocumentDataAfterAutoGeneratingId.

/**
 * Add data to a document after generating the document id.
 *
 * @return auto generated id
 */
String addDocumentDataAfterAutoGeneratingId() throws Exception {
    City data = new City();
    // [START fs_add_doc_data_after_auto_id]
    // Add document data after generating an id.
    DocumentReference addedDocRef = db.collection("cities").document();
    System.out.println("Added document with ID: " + addedDocRef.getId());
    // later...
    ApiFuture<WriteResult> writeResult = addedDocRef.set(data);
    // [END fs_add_doc_data_after_auto_id]
    // writeResult.get() blocks on operation
    System.out.println("Update time : " + writeResult.get().getUpdateTime());
    return addedDocRef.getId();
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 25 with DocumentReference

use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method writeBatch.

/**
 * Write documents in a batch.
 */
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]
    // Get a new write batch
    WriteBatch batch = db.batch();
    // Set the value of 'NYC'
    DocumentReference nycRef = db.collection("cities").document("NYC");
    batch.set(nycRef, new City());
    // Update the population of 'SF'
    DocumentReference sfRef = db.collection("cities").document("SF");
    batch.update(sfRef, "population", 1000000L);
    // Delete the city 'LA'
    DocumentReference laRef = db.collection("cities").document("LA");
    batch.delete(laRef);
    // asynchronously commit the batch
    ApiFuture<List<WriteResult>> future = batch.commit();
    // future.get() blocks on batch commit operation
    for (WriteResult result : future.get()) {
        System.out.println("Update time : " + result.getUpdateTime());
    }
// [END fs_write_batch]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) ArrayList(java.util.ArrayList) List(java.util.List) City(com.example.firestore.snippets.model.City) WriteBatch(com.google.cloud.firestore.WriteBatch) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

DocumentReference (com.google.cloud.firestore.DocumentReference)26 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)14 Test (org.junit.Test)14 City (com.example.firestore.snippets.model.City)12 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)7 WriteResult (com.google.cloud.firestore.WriteResult)7 HashMap (java.util.HashMap)5 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)4 Transaction (com.google.cloud.firestore.Transaction)2 CollectionReference (com.google.cloud.firestore.CollectionReference)1 FirestoreException (com.google.cloud.firestore.FirestoreException)1 WriteBatch (com.google.cloud.firestore.WriteBatch)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1