Search in sources :

Example 91 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class QueryDataSnippets method createQuery.

/**
 * Creates a sample query.
 *
 * @return query
 */
Query createQuery() throws Exception {
    // [START fs_create_query]
    // [START firestore_query_filter_eq_boolean]
    // Create a reference to the cities collection
    CollectionReference cities = db.collection("cities");
    // Create a query against the collection.
    Query query = cities.whereEqualTo("capital", true);
    // 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]
    return query;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 92 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class QueryDataSnippets method createQueryAlternate.

/**
 * Creates a sample query.
 *
 * @return query
 */
Query createQueryAlternate() throws Exception {
    // [START fs_create_query_country]
    // [START firestore_query_filter_eq_string]
    // 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 : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 93 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class QueryDataSnippets method collectionGroupQuery.

void collectionGroupQuery() throws ExecutionException, InterruptedException {
    // CHECKSTYLE OFF: Indentation
    // CHECKSTYLE OFF: RightCurlyAlone
    // [START fs_collection_group_query_data_setup]
    // [START firestore_query_collection_group_dataset]
    CollectionReference cities = db.collection("cities");
    final List<ApiFuture<WriteResult>> futures = Arrays.asList(cities.document("SF").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "Golden Gate Bridge");
            put("type", "bridge");
        }
    }), cities.document("SF").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "Legion of Honor");
            put("type", "museum");
        }
    }), cities.document("LA").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "Griffith Park");
            put("type", "park");
        }
    }), cities.document("LA").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "The Getty");
            put("type", "museum");
        }
    }), cities.document("DC").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "Lincoln Memorial");
            put("type", "memorial");
        }
    }), cities.document("DC").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "National Air and Space Museum");
            put("type", "museum");
        }
    }), cities.document("TOK").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "Ueno Park");
            put("type", "park");
        }
    }), cities.document("TOK").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "National Museum of Nature and Science");
            put("type", "museum");
        }
    }), cities.document("BJ").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "Jingshan Park");
            put("type", "park");
        }
    }), cities.document("BJ").collection("landmarks").document().set(new HashMap<String, String>() {

        {
            put("name", "Beijing Ancient Observatory");
            put("type", "museum");
        }
    }));
    final List<WriteResult> landmarks = ApiFutures.allAsList(futures).get();
    // [END firestore_query_collection_group_dataset]
    // [END fs_collection_group_query_data_setup]
    // [START fs_collection_group_query]
    // [START firestore_query_collection_group_filter_eq]
    final Query museums = db.collectionGroup("landmarks").whereEqualTo("type", "museum");
    final ApiFuture<QuerySnapshot> querySnapshot = museums.get();
    for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
        System.out.println(document.getId());
    }
// [END firestore_query_collection_group_filter_eq]
// [END fs_collection_group_query]
// CHECKSTYLE ON: RightCurlyAlone
// CHECKSTYLE ON: Indentation
}
Also used : ApiFuture(com.google.api.core.ApiFuture) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) WriteResult(com.google.cloud.firestore.WriteResult) Query(com.google.cloud.firestore.Query) HashMap(java.util.HashMap) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 94 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class QueryDataSnippets method createStartAtSnapshotQueryCursor.

/**
 * Create a query using a snapshot as a start point.
 *
 * @return query
 */
Query createStartAtSnapshotQueryCursor() throws InterruptedException, ExecutionException, TimeoutException {
    // [START fs_document_snapshot_cursor]
    // [START firestore_query_cursor_start_at_document]
    // Fetch the snapshot with an API call, waiting for a maximum of 30 seconds for a result.
    ApiFuture<DocumentSnapshot> future = db.collection("cities").document("SF").get();
    DocumentSnapshot snapshot = future.get(30, TimeUnit.SECONDS);
    // Construct the query
    Query query = db.collection("cities").orderBy("population").startAt(snapshot);
    // [END fs_document_snapshot_cursor]
    return query;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Query(com.google.cloud.firestore.Query)

Example 95 with DocumentSnapshot

use of com.google.cloud.firestore.DocumentSnapshot in project java-firestore by googleapis.

the class RetrieveDataSnippets method getDocumentAsMap.

/**
 * Retrieves document in collection as map.
 *
 * @return map (string => object)
 */
public Map<String, Object> getDocumentAsMap() throws Exception {
    // [START fs_get_doc_as_map]
    // [START firestore_data_get_as_map]
    DocumentReference docRef = db.collection("cities").document("SF");
    // asynchronously retrieve the document
    ApiFuture<DocumentSnapshot> future = docRef.get();
    // ...
    // future.get() blocks on response
    DocumentSnapshot document = future.get();
    if (document.exists()) {
        System.out.println("Document data: " + document.getData());
    } else {
        System.out.println("No such document!");
    }
    // [END fs_get_doc_as_map]
    return (document.exists()) ? document.getData() : null;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)103 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)73 Test (org.junit.Test)52 DocumentReference (com.google.cloud.firestore.DocumentReference)49 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)23 ExecutionException (java.util.concurrent.ExecutionException)13 CollectionReference (com.google.cloud.firestore.CollectionReference)12 Query (com.google.cloud.firestore.Query)12 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)10 SingleField (com.google.cloud.firestore.LocalFirestoreHelper.SingleField)9 WriteResult (com.google.cloud.firestore.WriteResult)9 ArrayList (java.util.ArrayList)8 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)6 HashMap (java.util.HashMap)6 BulkWriter (com.google.cloud.firestore.BulkWriter)5 City (com.example.firestore.snippets.model.City)4 Timestamp (com.google.cloud.Timestamp)4 FirestoreBundleTest.verifyNamedQuery (com.google.cloud.firestore.FirestoreBundleTest.verifyNamedQuery)4 AllSupportedTypes (com.google.cloud.firestore.LocalFirestoreHelper.AllSupportedTypes)4 WriteBatch (com.google.cloud.firestore.WriteBatch)4