本文整理汇总了Java中com.google.cloud.firestore.WriteResult类的典型用法代码示例。如果您正苦于以下问题:Java WriteResult类的具体用法?Java WriteResult怎么用?Java WriteResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WriteResult类属于com.google.cloud.firestore包,在下文中一共展示了WriteResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testFirestoreAccess
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
@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());
}
示例2: addSimpleDocumentAsMap
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/**
* 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");
// 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;
}
示例3: addDocumentWithDifferentDataTypes
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/**
* 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;
}
示例4: addDocumentDataAfterAutoGeneratingId
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/**
* 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();
}
示例5: updateUsingMap
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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]
}
示例6: updateAndCreateIfMissing
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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]
}
示例7: prepareExamples
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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]
}
示例8: prepareExamples
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/**
* Creates cities collection and add sample documents to test queries.
*
* @return collection reference
*/
void prepareExamples() throws Exception {
// [START fs_query_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 documents successfully added
ApiFutures.allAsList(futures).get();
// [END fs_query_create_examples]
}
示例9: addSimpleDocumentAsEntity
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/**
* 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);
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;
}
示例10: updateSimpleDocument
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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]
}
示例11: updateNestedFields
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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
}
示例12: updateServerTimestamp
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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]
}
示例13: deleteFields
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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]
}
示例14: deleteDocument
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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]
}
示例15: writeBatch
import com.google.cloud.firestore.WriteResult; //导入依赖的package包/类
/** 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]
}