use of com.google.cloud.firestore.QuerySnapshot in project java-docs-samples by GoogleCloudPlatform.
the class Quickstart method runAQuery.
void runAQuery() throws Exception {
// [START fs_add_query]
// asynchronously query for all users born before 1900
ApiFuture<QuerySnapshot> query = db.collection("users").whereLessThan("born", 1900).get();
// ...
// query.get() blocks on response
QuerySnapshot querySnapshot = query.get();
List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
for (QueryDocumentSnapshot document : documents) {
System.out.println("User: " + document.getId());
System.out.println("First: " + document.getString("first"));
if (document.contains("middle")) {
System.out.println("Middle: " + document.getString("middle"));
}
System.out.println("Last: " + document.getString("last"));
System.out.println("Born: " + document.getLong("born"));
}
// [END fs_add_query]
}
use of com.google.cloud.firestore.QuerySnapshot in project java-docs-samples by GoogleCloudPlatform.
the class ListenDataSnippets method detachListener.
/**
* Demonstrate how to detach an event listener.
*/
void detachListener() {
// [START detach_errors]
Query query = db.collection("cities");
ListenerRegistration registration = query.addSnapshotListener(new EventListener<QuerySnapshot>() {
// [START_EXCLUDE]
@Override
public void onEvent(@Nullable QuerySnapshot snapshots, @Nullable FirestoreException e) {
}
});
// ...
// Stop listening to changes
registration.remove();
// [END detach_errors]
}
use of com.google.cloud.firestore.QuerySnapshot in project java-docs-samples by GoogleCloudPlatform.
the class QueryDataSnippets method createAQuery.
/**
* Creates a sample query.
*
* @return query
*/
Query createAQuery() throws Exception {
// [START fs_create_query]
// 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;
}
use of com.google.cloud.firestore.QuerySnapshot in project java-docs-samples by GoogleCloudPlatform.
the class RetrieveDataSnippets method getAllDocuments.
/**
* Return all documents in the cities collection.
*
* @return list of documents
*/
public List<QueryDocumentSnapshot> getAllDocuments() throws Exception {
// [START fs_get_all_docs]
// asynchronously retrieve all documents
ApiFuture<QuerySnapshot> future = db.collection("cities").get();
// future.get() blocks on response
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
System.out.println(document.getId() + " => " + document.toObject(City.class));
}
// [END fs_get_all_docs]
return documents;
}
use of com.google.cloud.firestore.QuerySnapshot in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method deleteCollection.
// [START fs_delete_collection]
/**
* Delete a collection in batches to avoid out-of-memory errors.
* Batch size may be tuned based on document size (atmost 1MB) and application requirements.
*/
void deleteCollection(CollectionReference collection, int batchSize) {
try {
// retrieve a small batch of documents to avoid out-of-memory errors
ApiFuture<QuerySnapshot> future = collection.limit(batchSize).get();
int deleted = 0;
// future.get() blocks on document retrieval
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
document.getReference().delete();
++deleted;
}
if (deleted >= batchSize) {
// retrieve and delete another batch
deleteCollection(collection, batchSize);
}
} catch (Exception e) {
System.err.println("Error deleting collection : " + e.getMessage());
}
}
Aggregations