use of com.google.cloud.firestore.CollectionReference in project java-docs-samples by GoogleCloudPlatform.
the class QueryDataSnippets method createOrderByNameWithLimitQuery.
/**
* Creates a query that combines order by with limit.
*
* @return query
*/
Query createOrderByNameWithLimitQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_order_by_name_limit_query]
Query query = cities.orderBy("name").limit(3);
// [END fs_order_by_name_limit_query]
return query;
}
use of com.google.cloud.firestore.CollectionReference in project java-docs-samples by GoogleCloudPlatform.
the class QueryDataSnippets method createOrderByNameDescWithLimitQuery.
/**
* Creates a query that combines order by in descending order with the limit operator.
*
* @return query
*/
Query createOrderByNameDescWithLimitQuery() {
CollectionReference cities = db.collection("cities");
// [START fs_order_by_name_desc_limit_query]
Query query = cities.orderBy("name", Direction.DESCENDING).limit(3);
// [END fs_order_by_name_desc_limit_query]
return query;
}
use of com.google.cloud.firestore.CollectionReference in project java-docs-samples by GoogleCloudPlatform.
the class RetrieveDataSnippets method prepareExamples.
/**
* Create cities collection and add sample documents.
*/
void prepareExamples() throws Exception {
// [START fs_retrieve_create_examples]
CollectionReference cities = db.collection("cities");
List<ApiFuture<WriteResult>> futures = new ArrayList<>();
futures.add(cities.document("SF").set(new City("San Francisco", "CA", "USA", false, 860000L)));
futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L)));
futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L)));
futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L)));
futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L)));
// (optional) block on operation
ApiFutures.allAsList(futures).get();
// [END fs_retrieve_create_examples]
}
use of com.google.cloud.firestore.CollectionReference in project spring-cloud-gcp by spring-cloud.
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 testcontainers-java by testcontainers.
the class FirestoreEmulatorContainerTest method testSimple.
// }
// testWithEmulatorContainer {
@Test
public void testSimple() throws ExecutionException, InterruptedException {
FirestoreOptions options = FirestoreOptions.getDefaultInstance().toBuilder().setHost(emulator.getEmulatorEndpoint()).setCredentials(NoCredentials.getInstance()).setProjectId("test-project").build();
Firestore firestore = options.getService();
CollectionReference users = firestore.collection("users");
DocumentReference docRef = users.document("alovelace");
Map<String, Object> data = new HashMap<>();
data.put("first", "Ada");
data.put("last", "Lovelace");
ApiFuture<WriteResult> result = docRef.set(data);
result.get();
ApiFuture<QuerySnapshot> query = users.get();
QuerySnapshot querySnapshot = query.get();
assertThat(querySnapshot.getDocuments().get(0).getData()).containsEntry("first", "Ada");
}
Aggregations