com.google.cloud.firestore.WriteResult Java Examples
The following examples show how to use
com.google.cloud.firestore.WriteResult.
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: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** Partially update fields of a document using a map (field => value). */ void updateAndCreateIfMissing() throws Exception { // [START fs_update_create_if_missing] //asynchronously update doc, create the document if missing Map<String, Object> update = new HashMap<>(); update.put("capital", true); ApiFuture<WriteResult> writeResult = db .collection("cities") .document("BJ") .set(update, SetOptions.merge()); // ... System.out.println("Update time : " + writeResult.get().getUpdateTime()); // [END fs_update_create_if_missing] }
Example #2
Source File: RetrieveDataSnippets.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** 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, Arrays.asList("west_coast", "norcal")))); futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L, Arrays.asList("west_coast", "socal")))); futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L, Arrays.asList("east_coast")))); futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L, Arrays.asList("kanto", "honshu")))); futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L, Arrays.asList("jingjinji", "hebei")))); // (optional) block on operation ApiFutures.allAsList(futures).get(); // [END fs_retrieve_create_examples] }
Example #3
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * Add a document to a collection using a map. * * @return document data */ Map<String, Object> addSimpleDocumentAsMap() throws Exception { // [START fs_add_doc_as_map] // Create a Map to store the data we want to set Map<String, Object> docData = new HashMap<>(); docData.put("name", "Los Angeles"); docData.put("state", "CA"); docData.put("country", "USA"); docData.put("regions", Arrays.asList("west_coast", "socal")); // Add a new document (asynchronously) in collection "cities" with id "LA" ApiFuture<WriteResult> future = db.collection("cities").document("LA").set(docData); // ... // future.get() blocks on response System.out.println("Update time : " + future.get().getUpdateTime()); // [END fs_add_doc_as_map] return docData; }
Example #4
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * Add a document to a collection using a map with different supported data types. * * @return document data */ Map<String, Object> addDocumentWithDifferentDataTypes() throws Exception { // [START fs_add_doc_data_types] Map<String, Object> docData = new HashMap<>(); docData.put("stringExample", "Hello, World"); docData.put("booleanExample", false); docData.put("numberExample", 3.14159265); docData.put("nullExample", null); ArrayList<Object> arrayExample = new ArrayList<>(); Collections.addAll(arrayExample, 5L, true, "hello"); docData.put("arrayExample", arrayExample); Map<String, Object> objectExample = new HashMap<>(); objectExample.put("a", 5L); objectExample.put("b", true); docData.put("objectExample", objectExample); ApiFuture<WriteResult> future = db.collection("data").document("one").set(docData); System.out.println("Update time : " + future.get().getUpdateTime()); // [END fs_add_doc_data_types] return docData; }
Example #5
Source File: FirestoreClientIT.java From firebase-admin-java with Apache License 2.0 | 6 votes |
@Test public void testFirestoreAccess() throws Exception { Firestore firestore = FirestoreClient.getFirestore(IntegrationTestUtils.ensureDefaultApp()); DocumentReference reference = firestore.collection("cities").document("Mountain View"); ImmutableMap<String, Object> expected = ImmutableMap.<String, Object>of( "name", "Mountain View", "country", "USA", "population", 77846L, "capital", false ); WriteResult result = reference.set(expected).get(); assertNotNull(result); Map<String, Object> data = reference.get().get().getData(); assertEquals(expected.size(), data.size()); for (Map.Entry<String, Object> entry : expected.entrySet()) { assertEquals(entry.getValue(), data.get(entry.getKey())); } reference.delete().get(); assertFalse(reference.get().get().exists()); }
Example #6
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** * Add data to a document after generating the document id. * * @return auto generated id */ String addDocumentDataAfterAutoGeneratingId() throws Exception { City data = new City(); // [START fs_add_doc_data_after_auto_id] // Add document data after generating an id. DocumentReference addedDocRef = db.collection("cities").document(); System.out.println("Added document with ID: " + addedDocRef.getId()); // later... ApiFuture<WriteResult> writeResult = addedDocRef.set(data); // [END fs_add_doc_data_after_auto_id] // writeResult.get() blocks on operation System.out.println("Update time : " + writeResult.get().getUpdateTime()); return addedDocRef.getId(); }
Example #7
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** Partially update fields of a document using a map (field => value). */ void updateUsingMap() throws Exception { db.collection("cities").document("DC").set(new City("Washington D.C.")).get(); // [START fs_update_doc_map] // update multiple fields using a map DocumentReference docRef = db.collection("cities").document("DC"); Map<String, Object> updates = new HashMap<>(); updates.put("name", "Washington D.C."); updates.put("country", "USA"); updates.put("capital", true); //asynchronously update doc ApiFuture<WriteResult> writeResult = docRef.update(updates); // ... System.out.println("Update time : " + writeResult.get().getUpdateTime()); // [END fs_update_doc_map] }
Example #8
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 6 votes |
/** Update array fields in a document. **/ void updateDocumentArray() throws Exception { // [START fs_update_document_array] DocumentReference washingtonRef = db.collection("cities").document("DC"); // Atomically add a new region to the "regions" array field. ApiFuture<WriteResult> arrayUnion = washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia")); System.out.println("Update time : " + arrayUnion.get()); // Atomically remove a region from the "regions" array field. ApiFuture<WriteResult> arrayRm = washingtonRef.update("regions", FieldValue.arrayRemove("east_coast")); System.out.println("Update time : " + arrayRm.get()); // [END fs_update_document_array] }
Example #9
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** Partial update nested fields of a document. */ void updateNestedFields() throws Exception { //CHECKSTYLE OFF: VariableDeclarationUsageDistance // [START fs_update_nested_fields] // Create an initial document to update DocumentReference frankDocRef = db.collection("users").document("frank"); Map<String, Object> initialData = new HashMap<>(); initialData.put("name", "Frank"); initialData.put("age", 12); Map<String, Object> favorites = new HashMap<>(); favorites.put("food", "Pizza"); favorites.put("color", "Blue"); favorites.put("subject", "Recess"); initialData.put("favorites", favorites); ApiFuture<WriteResult> initialResult = frankDocRef.set(initialData); // Confirm that data has been successfully saved by blocking on the operation initialResult.get(); // Update age and favorite color Map<String, Object> updates = new HashMap<>(); updates.put("age", 13); updates.put("favorites.color", "Red"); // Async update document ApiFuture<WriteResult> writeResult = frankDocRef.update(updates); // ... System.out.println("Update time : " + writeResult.get().getUpdateTime()); // [END fs_update_nested_fields] //CHECKSTYLE ON: VariableDeclarationUsageDistance }
Example #10
Source File: FirestoreDocumentationTests.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
void writeDocumentFromObject() throws ExecutionException, InterruptedException { // Add document data with id "joe" using a custom User class User data = new User("Joe", Arrays.asList( new Phone(12345, PhoneType.CELL), new Phone(54321, PhoneType.WORK))); // .get() blocks on response WriteResult writeResult = this.firestore.document("users/joe").set(data).get(); LOGGER.info("Update time: " + writeResult.getUpdateTime()); }
Example #11
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** Update document with server timestamp. */ void updateServerTimestamp() throws Exception { db.collection("objects").document("some-id").set(new HashMap<String, Object>()).get(); // [START fs_update_server_timestamp] DocumentReference docRef = db.collection("objects").document("some-id"); // Update the timestamp field with the value from the server ApiFuture<WriteResult> writeResult = docRef.update("timestamp", FieldValue.serverTimestamp()); System.out.println("Update time : " + writeResult.get()); // [END fs_update_server_timestamp] }
Example #12
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** Partially update a document using the .update(field1, value1..) method. */ void updateSimpleDocument() throws Exception { db.collection("cities").document("DC").set(new City("Washington D.C.")).get(); // [START fs_update_doc] // Update an existing document DocumentReference docRef = db.collection("cities").document("DC"); // (async) Update one field ApiFuture<WriteResult> future = docRef.update("capital", true); // ... WriteResult result = future.get(); System.out.println("Write result: " + result); // [END fs_update_doc] }
Example #13
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** * Add a document to a collection as a custom object. * * @return entity added */ City addSimpleDocumentAsEntity() throws Exception { // [START fs_add_simple_doc_as_entity] City city = new City("Los Angeles", "CA", "USA", false, 3900000L, Arrays.asList("west_coast", "socal")); ApiFuture<WriteResult> future = db.collection("cities").document("LA").set(city); // block on response if required System.out.println("Update time : " + future.get().getUpdateTime()); // [END fs_add_simple_doc_as_entity] return city; }
Example #14
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** Delete specific fields when updating a document. */ void deleteFields() throws Exception { City city = new City("Beijing"); city.setCapital(true); db.collection("cities").document("BJ").set(city).get(); // [START fs_delete_fields] DocumentReference docRef = db.collection("cities").document("BJ"); Map<String, Object> updates = new HashMap<>(); updates.put("capital", FieldValue.delete()); // Update and delete the "capital" field in the document ApiFuture<WriteResult> writeResult = docRef.update(updates); System.out.println("Update time : " + writeResult.get()); // [END fs_delete_fields] }
Example #15
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** Delete a document in a collection. */ void deleteDocument() throws Exception { db.collection("cities").document("DC").set(new City("Washington, D.C.")).get(); // [START fs_delete_doc] // asynchronously delete a document ApiFuture<WriteResult> writeResult = db.collection("cities").document("DC").delete(); // ... System.out.println("Update time : " + writeResult.get().getUpdateTime()); // [END fs_delete_doc] }
Example #16
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 5 votes |
/** Write documents in a batch. */ void writeBatch() throws Exception { db.collection("cities").document("SF").set(new City()).get(); db.collection("cities").document("LA").set(new City()).get(); // [START fs_write_batch] // Get a new write batch WriteBatch batch = db.batch(); // Set the value of 'NYC' DocumentReference nycRef = db.collection("cities").document("NYC"); batch.set(nycRef, new City()); // Update the population of 'SF' DocumentReference sfRef = db.collection("cities").document("SF"); batch.update(sfRef, "population", 1000000L); // Delete the city 'LA' DocumentReference laRef = db.collection("cities").document("LA"); batch.delete(laRef); // asynchronously commit the batch ApiFuture<List<WriteResult>> future = batch.commit(); // ... // future.get() blocks on batch commit operation for (WriteResult result :future.get()) { System.out.println("Update time : " + result.getUpdateTime()); } // [END fs_write_batch] }
Example #17
Source File: ManageDataSnippets.java From java-docs-samples with Apache License 2.0 | 5 votes |
public void updateDocumentIncrement() throws ExecutionException, InterruptedException { final City city = new City(); city.setPopulation(100L); db.collection("cities").document("DC").set(city).get(); // [START fs_update_document_increment] DocumentReference washingtonRef = db.collection("cities").document("DC"); // Atomically increment the population of the city by 50. final ApiFuture<WriteResult> updateFuture = washingtonRef .update("population", FieldValue.increment(50)); // [END fs_update_document_increment] updateFuture.get(); }
Example #18
Source File: FirestoreDocumentationTests.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
void writeDocumentFromObject2() throws ExecutionException, InterruptedException { // Add document data with id "joe" using a custom User class User data = new User("Joseph", Arrays.asList( new Phone(23456, PhoneType.CELL), new Phone(65432, PhoneType.WORK))); // .get() blocks on response WriteResult writeResult = this.firestore.document("users/joe").set(data).get(); LOGGER.info("Update time: " + writeResult.getUpdateTime()); }
Example #19
Source File: FirestoreSampleApp.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private void writeDocumentFromObject() throws ExecutionException, InterruptedException { // Add document data with id "joe" using a custom User class User data = new User("Joe", Arrays.asList(new Phone(12345, PhoneType.CELL), new Phone(54321, PhoneType.WORK))); // .get() blocks on response WriteResult writeResult = this.firestore.document("users/joe").set(data).get(); System.out.println("Update time: " + writeResult.getUpdateTime()); }
Example #20
Source File: FirestoreSampleApp.java From spring-cloud-gcp with Apache License 2.0 | 5 votes |
private void writeDocumentFromMap() throws InterruptedException, java.util.concurrent.ExecutionException { DocumentReference docRef = this.firestore.collection("users").document("ada"); // Add document data with id "ada" using a hashmap Map<String, Object> data = new HashMap<>(); data.put("name", "Ada"); data.put("phones", Arrays.asList(123, 456)); // asynchronously write data ApiFuture<WriteResult> result = docRef.set(data); // result.get() blocks on response System.out.println("Update time: " + result.get().getUpdateTime()); }
Example #21
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 5 votes |
public WriteResult setDocument(String path, Map map) { try { return setDocumentAsync(path, map).get(); } catch (ExecutionException | InterruptedException e) { throw new IllegalStateException(e); } }
Example #22
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 5 votes |
public WriteResult deleteDocument(String path) { try { return deleteDocumentAsync(path).get(); } catch (ExecutionException | InterruptedException e) { throw new IllegalStateException(e); } }
Example #23
Source File: FirestoreClientTool.java From startup-os with Apache License 2.0 | 5 votes |
public void testFunctionality() { WriteResult result = client.setProtoDocument("test/bla", Diff.newBuilder().setId(123).build()); System.out.println("Update time : " + result.getUpdateTime()); client.addCollectionListener( "/reviewer/ci/requests", CiRequest.newBuilder(), new ProtoEventListener<ProtoQuerySnapshot<CiRequest>>() { @Override public void onEvent( @Nullable ProtoQuerySnapshot<CiRequest> snapshot, @Nullable RuntimeException e) { if (e != null) { System.err.println("Listen failed: " + e); return; } ImmutableList<CiRequest> protos = snapshot.getProtos(); for (CiRequest ciRequest : protos) { System.out.println(ciRequest); } ImmutableList<ProtoChange<CiRequest>> protoChanges = snapshot.getProtoChanges(); for (ProtoChange<CiRequest> protoChange : protoChanges) { System.out.println(protoChange.getProto()); System.out.println(protoChange.getType()); System.out.println(protoChange.getOldIndex()); System.out.println(protoChange.getNewIndex()); } } }); }
Example #24
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 5 votes |
public ApiFuture<WriteResult> setProtoDocumentAsync(String path, Message proto) { try { return setDocumentAsync(path, encodeProto(proto)); } catch (InvalidProtocolBufferException e) { throw new IllegalStateException(e); } }
Example #25
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 5 votes |
public WriteResult setProtoDocument(String path, Message proto) { try { return setProtoDocumentAsync(path, proto).get(); } catch (ExecutionException | InterruptedException e) { throw new IllegalStateException(e); } }
Example #26
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 4 votes |
public ApiFuture<WriteResult> deleteDocumentAsync(String path) { return getDocumentReference(path).delete(); }
Example #27
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 4 votes |
public ApiFuture<WriteResult> setDocumentAsync(String path, Map map) { return getDocumentReference(path).set(map); }
Example #28
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 4 votes |
public ApiFuture<WriteResult> setDocumentAsync( String collection, String documentId, Map<String, ?> map) { return setDocumentAsync(joinPath(collection, documentId), map); }
Example #29
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 4 votes |
public WriteResult setDocument(String collection, String documentId, Map map) { return setDocument(joinPath(collection, documentId), map); }
Example #30
Source File: FirestoreProtoClient.java From startup-os with Apache License 2.0 | 4 votes |
public ApiFuture<WriteResult> setProtoDocumentAsync( String collection, String documentId, Message proto) { return setProtoDocumentAsync(joinPath(collection, documentId), proto); }