Search in sources :

Example 36 with CollectionReference

use of com.google.cloud.firestore.CollectionReference in project native-image-support-java by GoogleCloudPlatform.

the class FirestoreSampleApplication method createUserDocumentPojo.

private static void createUserDocumentPojo(Firestore db) throws Exception {
    CollectionReference collectionReference = db.collection(USERS_COLLECTION);
    WriteResult result = collectionReference.document().set(new Person("Alan", "Turing", 1912)).get();
    System.out.println("Created user by POJO. Timestamp: " + result.getUpdateTime());
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 37 with CollectionReference

use of com.google.cloud.firestore.CollectionReference 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 38 with CollectionReference

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

the class QueryDataSnippets method createRangeQuery.

/**
 * An instance of a valid range/inequality query : range operators are limited to a single field.
 *
 * @return query
 */
Query createRangeQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_range_query]
    Query validQuery1 = cities.whereGreaterThanOrEqualTo("state", "CA").whereLessThanOrEqualTo("state", "IN");
    Query validQuery2 = cities.whereEqualTo("state", "CA").whereGreaterThan("population", 1000000);
    // [END fs_range_query]
    return validQuery1;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 39 with CollectionReference

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

the class QueryDataSnippets method createInvalidRangeQuery.

/**
 * An instance of an invalid range query : range operators are limited to a single field.
 *
 * @return query
 */
Query createInvalidRangeQuery() {
    CollectionReference cities = db.collection("cities");
    // Violates constraint : range operators are limited to a single field
    // [START fs_invalid_range_query]
    Query invalidRangeQuery = cities.whereGreaterThanOrEqualTo("state", "CA").whereGreaterThan("population", 100000);
    // [END fs_invalid_range_query]
    return invalidRangeQuery;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 40 with CollectionReference

use of com.google.cloud.firestore.CollectionReference 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;
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

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