Search in sources :

Example 41 with CollectionReference

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

the class QueryDataSnippets method createOrderByNameWithLimitQuery.

/**
 * Creates a query that combines order by with limit.
 *
 * @return query
 */
Query createOrderByNameWithLimitQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_order_by_name_limit_query]
    Query query = cities.orderBy("name").limit(3);
    // [END fs_order_by_name_limit_query]
    return query;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 42 with CollectionReference

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

the class QueryDataSnippets method createOrderByNameDescWithLimitQuery.

/**
 * Creates a query that combines order by in descending order with the limit operator.
 *
 * @return query
 */
Query createOrderByNameDescWithLimitQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_order_by_name_desc_limit_query]
    Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
    // [END fs_order_by_name_desc_limit_query]
    return query;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 43 with CollectionReference

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

the class RetrieveDataSnippets method prepareExamples.

/**
 * Create cities collection and add sample documents.
 */
void prepareExamples() throws Exception {
    // [START fs_retrieve_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 operation
    ApiFutures.allAsList(futures).get();
// [END fs_retrieve_create_examples]
}
Also used : ApiFuture(com.google.api.core.ApiFuture) ArrayList(java.util.ArrayList) City(com.example.firestore.snippets.model.City) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 44 with CollectionReference

use of com.google.cloud.firestore.CollectionReference in project spring-cloud-gcp by spring-cloud.

the class FirestoreSampleApp method removeDocuments.

private void removeDocuments() {
    // Warning: Deleting a document does not delete its subcollections!
    // 
    // If you want to delete documents in subcollections when deleting a document, you must do so manually.
    // See https://firebase.google.com/docs/firestore/manage-data/delete-data#collections
    CollectionReference users = this.firestore.collection("users");
    Iterable<DocumentReference> documentReferences = users.listDocuments();
    documentReferences.forEach(documentReference -> {
        System.out.println("removing: " + documentReference.getId());
        try {
            documentReference.delete().get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    });
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) CollectionReference(com.google.cloud.firestore.CollectionReference) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 45 with CollectionReference

use of com.google.cloud.firestore.CollectionReference in project testcontainers-java by testcontainers.

the class FirestoreEmulatorContainerTest method testSimple.

// }
// testWithEmulatorContainer {
@Test
public void testSimple() throws ExecutionException, InterruptedException {
    FirestoreOptions options = FirestoreOptions.getDefaultInstance().toBuilder().setHost(emulator.getEmulatorEndpoint()).setCredentials(NoCredentials.getInstance()).setProjectId("test-project").build();
    Firestore firestore = options.getService();
    CollectionReference users = firestore.collection("users");
    DocumentReference docRef = users.document("alovelace");
    Map<String, Object> data = new HashMap<>();
    data.put("first", "Ada");
    data.put("last", "Lovelace");
    ApiFuture<WriteResult> result = docRef.set(data);
    result.get();
    ApiFuture<QuerySnapshot> query = users.get();
    QuerySnapshot querySnapshot = query.get();
    assertThat(querySnapshot.getDocuments().get(0).getData()).containsEntry("first", "Ada");
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) HashMap(java.util.HashMap) Firestore(com.google.cloud.firestore.Firestore) CollectionReference(com.google.cloud.firestore.CollectionReference) DocumentReference(com.google.cloud.firestore.DocumentReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) FirestoreOptions(com.google.cloud.firestore.FirestoreOptions) Test(org.junit.Test)

Aggregations

CollectionReference (com.google.cloud.firestore.CollectionReference)72 Query (com.google.cloud.firestore.Query)48 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)16 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)12 DocumentReference (com.google.cloud.firestore.DocumentReference)11 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)11 ArrayList (java.util.ArrayList)11 ExecutionException (java.util.concurrent.ExecutionException)9 Test (org.junit.Test)9 WriteResult (com.google.cloud.firestore.WriteResult)7 DatasetDataProject (bio.terra.service.dataset.DatasetDataProject)6 ApiFuture (com.google.api.core.ApiFuture)6 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)5 HashMap (java.util.HashMap)5 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)4 City (com.example.firestore.snippets.model.City)4 Firestore (com.google.cloud.firestore.Firestore)4 FileSystemCorruptException (bio.terra.service.filedata.exception.FileSystemCorruptException)2 TranslateMessage (com.getstarted.background.objects.TranslateMessage)2 FirestoreOptions (com.google.cloud.firestore.FirestoreOptions)2