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");
}
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;
}
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);
}
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();
}
});
}
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);
}
Aggregations