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;
}
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;
}
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
}
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;
}
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;
}
Aggregations