Search in sources :

Example 11 with CollectionReference

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

the class QueryDataSnippets method createWhereWithOrderByAndLimitQuery.

/**
 * Creates a query that combines where clause with order by and limit operator.
 *
 * @return query
 */
Query createWhereWithOrderByAndLimitQuery() {
    CollectionReference cities = db.collection("cities");
    // [START fs_where_order_by_limit_query]
    Query query = cities.whereGreaterThan("population", 2500000L).orderBy("population").limit(2);
    // [END fs_where_order_by_limit_query]
    return query;
}
Also used : Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 12 with CollectionReference

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

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

the class RetrieveDataSnippetsIT method testGetSubcollections.

@Test
public void testGetSubcollections() throws Exception {
    // Add a landmark subcollection
    Map<String, String> data = new HashMap<>();
    data.put("foo", "bar");
    db.document("cities/SF/landmarks/example").set(data).get();
    Iterable<CollectionReference> collections = retrieveDataSnippets.getCollections();
    List<CollectionReference> collectionList = new ArrayList<>();
    for (CollectionReference collRef : collections) {
        collectionList.add(collRef);
    }
    assertEquals(collectionList.size(), 1);
    assertEquals(collectionList.get(0).getId(), "landmarks");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CollectionReference(com.google.cloud.firestore.CollectionReference) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 14 with CollectionReference

use of com.google.cloud.firestore.CollectionReference in project startup-os by google.

the class FirestoreProtoClient method getCollectionReference.

private CollectionReference getCollectionReference(String[] parts, int length) {
    DocumentReference docRef;
    CollectionReference collectionRef = client.collection(parts[0]);
    for (int i = 1; i < length; i += 2) {
        docRef = collectionRef.document(parts[i]);
        collectionRef = docRef.collection(parts[i + 1]);
    }
    return collectionRef;
}
Also used : DocumentReference(com.google.cloud.firestore.DocumentReference) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 15 with CollectionReference

use of com.google.cloud.firestore.CollectionReference in project cubanews by snava10.

the class HtmlCrawler method addDocMetadataIfDoesNotExists.

private ApiFuture<WriteResult> addDocMetadataIfDoesNotExists(IndexDocument document, Firestore db, String collectionName) throws Exception {
    CollectionReference collectionReference = db.collection(collectionName);
    Query query = collectionReference.whereEqualTo("url", document.url());
    if (query.get().get().getDocumentChanges().isEmpty()) {
        DocumentReference docRef = collectionReference.document();
        Map<String, Object> data = new HashMap<>();
        data.put("url", document.url());
        data.put("title", document.title());
        data.put("lastUpdated", document.lastUpdated());
        data.put("lastUpdatedReadable", LocalDateTime.ofEpochSecond(document.lastUpdated(), 0, ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        data.put("createdAt", document.lastUpdated());
        data.put("createdAtReadable", LocalDateTime.ofEpochSecond(document.lastUpdated(), 0, ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        data.put("state", DocumentState.ACTIVE.toString());
        return docRef.set(data);
    } else {
        System.out.printf("Article with url %s already exists%n", document.url());
    }
    return null;
}
Also used : Query(com.google.cloud.firestore.Query) HashMap(java.util.HashMap) CollectionReference(com.google.cloud.firestore.CollectionReference) DocumentReference(com.google.cloud.firestore.DocumentReference)

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