Java Code Examples for com.google.cloud.firestore.QuerySnapshot#getDocuments()
The following examples show how to use
com.google.cloud.firestore.QuerySnapshot#getDocuments() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyDataStore.java From smart-home-java with Apache License 2.0 | 6 votes |
public String getUserId(String token) throws ExecutionException, InterruptedException { if (token == null) { token = "Bearer 123access"; } ApiFuture<QuerySnapshot> userQuery = database.collection("users").whereEqualTo("fakeAccessToken", token.substring(7)).get(); QuerySnapshot usersSnapshot = userQuery.get(); List<QueryDocumentSnapshot> users = usersSnapshot.getDocuments(); DocumentSnapshot user; try { user = users.get(0); } catch (Exception e) { LOGGER.error("no user found!"); throw e; } return user.getId(); }
Example 2
Source File: ProtoQuerySnapshot.java From startup-os with Apache License 2.0 | 6 votes |
public ProtoQuerySnapshot(QuerySnapshot querySnapshot, Message.Builder builder) throws InvalidProtocolBufferException { // TODO: Avoid parsing the same objects twice in getDocuments() and getDocumentChanges(). ImmutableList.Builder<T> protos = ImmutableList.builder(); for (DocumentSnapshot docSnapshot : querySnapshot.getDocuments()) { protos.add((T) FirestoreProtoClient.parseProto(docSnapshot, builder)); } this.protos = protos.build(); ImmutableList.Builder<ProtoChange<T>> protoChanges = ImmutableList.builder(); for (DocumentChange docChange : querySnapshot.getDocumentChanges()) { T proto = (T) FirestoreProtoClient.parseProto(docChange.getDocument(), builder); protoChanges.add( new ProtoChange<T>( proto, docChange.getNewIndex(), docChange.getOldIndex(), convertChangeType(docChange.getType()))); } this.protoChanges = protoChanges.build(); }
Example 3
Source File: Guestbook.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** Query Firstore for Guestbook greetings */ public List<Greeting> getGreetings() { // Initialize a List for Greetings. ImmutableList.Builder<Greeting> greetings = new ImmutableList.Builder<Greeting>(); // Construct query. ApiFuture<QuerySnapshot> query = bookRef.collection("Greetings").orderBy("date", Direction.DESCENDING).get(); try { // Get query documents. QuerySnapshot querySnapshot = query.get(); for (QueryDocumentSnapshot greeting : querySnapshot.getDocuments()) { greetings.add(greeting.toObject(Greeting.class)); } } catch (Exception e) { System.out.println(e.getMessage()); } return greetings.build(); }
Example 4
Source File: Quickstart.java From java-docs-samples with Apache License 2.0 | 6 votes |
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] }
Example 5
Source File: Quickstart.java From java-docs-samples with Apache License 2.0 | 6 votes |
void retrieveAllDocuments() throws Exception { // [START fs_get_all] // asynchronously retrieve all users ApiFuture<QuerySnapshot> query = db.collection("users").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_get_all] }
Example 6
Source File: FirestoreSessionFilter.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Override public void init(FilterConfig config) throws ServletException { // Initialize local copy of datastore session variables. firestore = FirestoreOptions.getDefaultInstance().getService(); sessions = firestore.collection("sessions"); try { // Delete all sessions unmodified for over two days. Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.HOUR, -48); Date twoDaysAgo = Calendar.getInstance().getTime(); QuerySnapshot sessionDocs = sessions.whereLessThan("lastModified", dtf.format(twoDaysAgo)).get().get(); for (QueryDocumentSnapshot snapshot : sessionDocs.getDocuments()) { snapshot.getReference().delete(); } } catch (InterruptedException | ExecutionException e) { throw new ServletException("Exception initializing FirestoreSessionFilter.", e); } }
Example 7
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 5 votes |
public List<Message> getProtoDocuments(String path, Message.Builder builder) { ImmutableList.Builder<Message> result = ImmutableList.builder(); try { Message proto = builder.build(); QuerySnapshot querySnapshot = getDocumentsAsync(path).get(); for (QueryDocumentSnapshot document : querySnapshot.getDocuments()) { result.add(parseProto(document, builder)); } return result.build(); } catch (ExecutionException | InterruptedException | InvalidProtocolBufferException e) { throw new IllegalStateException(e); } }
Example 8
Source File: BaseIntegrationTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
protected static void deleteAllDocuments(Firestore db) throws Exception { ApiFuture<QuerySnapshot> future = db.collection("cities").get(); QuerySnapshot querySnapshot = future.get(); for (DocumentSnapshot doc : querySnapshot.getDocuments()) { // block on delete operation db.collection("cities").document(doc.getId()).delete().get(); } }
Example 9
Source File: QuickstartIT.java From java-docs-samples with Apache License 2.0 | 5 votes |
private void deleteAllDocuments() throws Exception { ApiFuture<QuerySnapshot> future = db.collection("users").get(); QuerySnapshot querySnapshot = future.get(); for (DocumentSnapshot doc : querySnapshot.getDocuments()) { // block on delete operation db.document("users/" + doc.getId()).delete().get(); } }
Example 10
Source File: QueryDataSnippetsIT.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testMultipleCursorConditions() throws Exception { // populate us_cities collection Map<String, String> city1 = new ImmutableMap.Builder<String, String>() .put("name", "Springfield").put("state", "Massachusetts").build(); Map<String, String> city2 = new ImmutableMap.Builder<String, String>() .put("name", "Springfield").put("state", "Missouri").build(); Map<String, String> city3 = new ImmutableMap.Builder<String, String>() .put("name", "Springfield").put("state", "Wisconsin").build(); db.collection("us_cities").document("Massachusetts").set(city1).get(); db.collection("us_cities").document("Missouri").set(city2).get(); db.collection("us_cities").document("Wisconsin").set(city3).get(); Query query1 = db.collection("us_cities") .orderBy("name") .orderBy("state") .startAt("Springfield"); // all documents are retrieved QuerySnapshot querySnapshot = query1.get().get(); List<QueryDocumentSnapshot> docs = querySnapshot.getDocuments(); assertEquals(3, docs.size()); // Will return "Springfield, Missouri" and "Springfield, Wisconsin" Query query2 = db.collection("us_cities") .orderBy("name") .orderBy("state") .startAt("Springfield", "Missouri"); // only Missouri and Wisconsin are retrieved List<String> expectedResults = Arrays.asList("Missouri", "Wisconsin"); List<String> result = getResults(query2); assertTrue(Objects.equals(result, expectedResults)); }
Example 11
Source File: QueryDataSnippetsIT.java From java-docs-samples with Apache License 2.0 | 5 votes |
private List<String> getResults(Query query) throws Exception { // asynchronously retrieve query results ApiFuture<QuerySnapshot> future = query.get(); // block on response QuerySnapshot querySnapshot = future.get(); List<String> docIds = new ArrayList<>(); for (DocumentSnapshot document : querySnapshot.getDocuments()) { docIds.add(document.getId()); } return docIds; }
Example 12
Source File: RetrieveDataSnippetsIT.java From java-docs-samples with Apache License 2.0 | 5 votes |
private static void deleteAllDocuments() throws Exception { ApiFuture<QuerySnapshot> future = db.collection("cities").get(); QuerySnapshot querySnapshot = future.get(); for (DocumentSnapshot doc : querySnapshot.getDocuments()) { // block on delete operation db.collection("cities").document(doc.getId()).delete().get(); } }