Search in sources :

Example 31 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 32 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 33 with CollectionReference

use of com.google.cloud.firestore.CollectionReference in project getting-started-java by GoogleCloudPlatform.

the class TranslateServlet method doGet.

// [START getting_started_background_app_list]
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore");
    CollectionReference translations = firestore.collection("translations");
    QuerySnapshot snapshot;
    try {
        snapshot = translations.limit(10).get().get();
    } catch (InterruptedException | ExecutionException e) {
        throw new ServletException("Exception retrieving documents from Firestore.", e);
    }
    List<TranslateMessage> translateMessages = Lists.newArrayList();
    List<QueryDocumentSnapshot> documents = Lists.newArrayList(snapshot.getDocuments());
    documents.sort(Comparator.comparing(DocumentSnapshot::getCreateTime));
    for (DocumentSnapshot document : Lists.reverse(documents)) {
        String encoded = gson.toJson(document.getData());
        TranslateMessage message = gson.fromJson(encoded, TranslateMessage.class);
        message.setData(decode(message.getData()));
        translateMessages.add(message);
    }
    req.setAttribute("messages", translateMessages);
    req.setAttribute("page", "list");
    req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) ServletException(javax.servlet.ServletException) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Firestore(com.google.cloud.firestore.Firestore) TranslateMessage(com.getstarted.background.objects.TranslateMessage) ExecutionException(java.util.concurrent.ExecutionException)

Example 34 with CollectionReference

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

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 35 with CollectionReference

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

the class FirestoreSampleApplicationNativeIntegrationTests method verifyUserPersisted.

private void verifyUserPersisted(User user) throws InterruptedException, ExecutionException {
    CollectionReference users = firestore.collection("users");
    DocumentReference docRef = users.document(user.getName());
    Map<String, Object> storedDoc = docRef.get().get().getData();
    assertThat(storedDoc).isNotNull().containsEntry("age", (long) user.getAge());
    List<Map<String, Object>> petsMapList = new ObjectMapper().convertValue(user.getPets(), new TypeReference<List<Map<String, Object>>>() {
    });
    assertThat(storedDoc.get("pets")).isNotNull().asList().hasSize(user.getPets().size()).containsAll(petsMapList);
}
Also used : Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Map(java.util.Map) CollectionReference(com.google.cloud.firestore.CollectionReference) DocumentReference(com.google.cloud.firestore.DocumentReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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