|
61 | 61 | import com.google.cloud.firestore.pipeline.stages.Unnest; |
62 | 62 | import com.google.cloud.firestore.pipeline.stages.UnnestOptions; |
63 | 63 | import com.google.cloud.firestore.pipeline.stages.Update; |
64 | | -import com.google.cloud.firestore.pipeline.stages.Upsert; |
65 | 64 | import com.google.cloud.firestore.pipeline.stages.Where; |
66 | 65 | import com.google.cloud.firestore.telemetry.MetricsUtil.MetricsContext; |
67 | 66 | import com.google.cloud.firestore.telemetry.TelemetryConstants; |
@@ -1002,105 +1001,224 @@ public Pipeline unnest(Selectable field, UnnestOptions options) { |
1002 | 1001 | /** |
1003 | 1002 | * Performs a delete operation on documents from previous stages. |
1004 | 1003 | * |
1005 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
| 1004 | + * <p> |
| 1005 | + * Example: |
| 1006 | + * |
| 1007 | + * <pre>{@code |
| 1008 | + * // Delete all documents in the "logs" collection where "status" is "archived" |
| 1009 | + * firestore.pipeline() |
| 1010 | + * .collection("logs") |
| 1011 | + * .where(field("status").equal("archived")) |
| 1012 | + * .delete() |
| 1013 | + * .execute() |
| 1014 | + * .get(); |
| 1015 | + * }</pre> |
| 1016 | + * |
| 1017 | + * @return A new {@code Pipeline} object with this stage appended to the stage |
| 1018 | + * list. |
1006 | 1019 | */ |
1007 | 1020 | @BetaApi |
1008 | 1021 | public Pipeline delete() { |
1009 | 1022 | return append(new Delete()); |
1010 | 1023 | } |
1011 | 1024 |
|
1012 | 1025 | /** |
1013 | | - * Performs an upsert operation using documents from previous stages. |
| 1026 | + * Performs an update operation using documents from previous stages. |
1014 | 1027 | * |
1015 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
1016 | | - */ |
1017 | | - @BetaApi |
1018 | | - public Pipeline upsert() { |
1019 | | - return append(new Upsert()); |
1020 | | - } |
1021 | | - |
1022 | | - /** |
1023 | | - * Performs an upsert operation using documents from previous stages. |
| 1028 | + * <p> |
| 1029 | + * This method updates the documents in place based on the data flowing through |
| 1030 | + * the pipeline. |
| 1031 | + * To specify transformations, use {@link #update(Selectable...)}. |
1024 | 1032 | * |
1025 | | - * @param target The collection to upsert to. |
1026 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
1027 | | - */ |
1028 | | - @BetaApi |
1029 | | - public Pipeline upsert(CollectionReference target) { |
1030 | | - return append(new Upsert().withCollection(target)); |
1031 | | - } |
1032 | | - |
1033 | | - /** |
1034 | | - * Performs an upsert operation using documents from previous stages. |
| 1033 | + * <p> |
| 1034 | + * Example 1: Update a collection's schema by adding a new field and removing |
| 1035 | + * an old one. |
1035 | 1036 | * |
1036 | | - * @param upsertStage The {@code Upsert} stage to append. |
1037 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
1038 | | - */ |
1039 | | - @InternalApi |
1040 | | - public Pipeline upsert(Upsert upsertStage) { |
1041 | | - return append(upsertStage); |
1042 | | - } |
1043 | | - |
1044 | | - /** |
1045 | | - * Performs an update operation using documents from previous stages. |
| 1037 | + * <pre>{@code |
| 1038 | + * firestore.pipeline() |
| 1039 | + * .collection("books") |
| 1040 | + * .addFields(constant("Fiction").as("genre")) |
| 1041 | + * .removeFields("old_genre") |
| 1042 | + * .update() |
| 1043 | + * .execute() |
| 1044 | + * .get(); |
| 1045 | + * }</pre> |
1046 | 1046 | * |
1047 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
| 1047 | + * <p> |
| 1048 | + * Example 2: Update documents in place with data from literals. |
| 1049 | + * |
| 1050 | + * <pre>{@code |
| 1051 | + * Map<String, Object> updateData = new HashMap<>(); |
| 1052 | + * updateData.put("__name__", firestore.collection("books").document("book1")); |
| 1053 | + * updateData.put("status", "Updated"); |
| 1054 | + * |
| 1055 | + * firestore.pipeline() |
| 1056 | + * .literals(updateData) |
| 1057 | + * .update() |
| 1058 | + * .execute() |
| 1059 | + * .get(); |
| 1060 | + * }</pre> |
| 1061 | + * |
| 1062 | + * @return A new {@code Pipeline} object with this stage appended to the stage |
| 1063 | + * list. |
1048 | 1064 | */ |
1049 | 1065 | @BetaApi |
1050 | 1066 | public Pipeline update() { |
1051 | 1067 | return append(new Update()); |
1052 | 1068 | } |
1053 | 1069 |
|
1054 | 1070 | /** |
1055 | | - * Performs an update operation using documents from previous stages. |
| 1071 | + * Performs an update operation using documents from previous stages with |
| 1072 | + * specified |
| 1073 | + * transformations. |
| 1074 | + * |
| 1075 | + * <p> |
| 1076 | + * Example: |
| 1077 | + * |
| 1078 | + * <pre>{@code |
| 1079 | + * // Update the "status" field to "Discounted" for all books where price > 50 |
| 1080 | + * firestore.pipeline() |
| 1081 | + * .collection("books") |
| 1082 | + * .where(field("price").greaterThan(50)) |
| 1083 | + * .update(constant("Discounted").as("status")) |
| 1084 | + * .execute() |
| 1085 | + * .get(); |
| 1086 | + * }</pre> |
1056 | 1087 | * |
1057 | 1088 | * @param transformations The transformations to apply. |
1058 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
| 1089 | + * @return A new {@code Pipeline} object with this stage appended to the stage |
| 1090 | + * list. |
1059 | 1091 | */ |
1060 | 1092 | @BetaApi |
1061 | 1093 | public Pipeline update(Selectable... transformations) { |
1062 | 1094 | return append(new Update().withTransformations(transformations)); |
1063 | 1095 | } |
1064 | 1096 |
|
1065 | 1097 | /** |
1066 | | - * Performs an update operation using documents from previous stages. |
| 1098 | + * Performs an update operation using an {@link Update} stage. |
| 1099 | + * |
| 1100 | + * <p> |
| 1101 | + * This method allows you to use a pre-configured {@link Update} stage. |
| 1102 | + * |
| 1103 | + * <p> |
| 1104 | + * Example: |
| 1105 | + * |
| 1106 | + * <pre>{@code |
| 1107 | + * Update updateStage = new Update().withTransformations(constant("Updated").as("status")); |
| 1108 | + * |
| 1109 | + * firestore.pipeline() |
| 1110 | + * .collection("books") |
| 1111 | + * .where(field("title").equal("The Hitchhiker's Guide to the Galaxy")) |
| 1112 | + * .update(updateStage) |
| 1113 | + * .execute() |
| 1114 | + * .get(); |
| 1115 | + * }</pre> |
1067 | 1116 | * |
1068 | 1117 | * @param update The {@code Update} stage to append. |
1069 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
| 1118 | + * @return A new {@code Pipeline} object with this stage appended to the stage |
| 1119 | + * list. |
1070 | 1120 | */ |
1071 | | - @InternalApi |
| 1121 | + @BetaApi |
1072 | 1122 | public Pipeline update(Update update) { |
1073 | 1123 | return append(update); |
1074 | 1124 | } |
1075 | 1125 |
|
1076 | 1126 | /** |
1077 | 1127 | * Performs an insert operation using documents from previous stages. |
1078 | 1128 | * |
1079 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
| 1129 | + * <p> |
| 1130 | + * The documents must include a valid {@code __name__} field specifying the |
| 1131 | + * document reference to insert. If the document already exists, the operation |
| 1132 | + * will fail. |
| 1133 | + * |
| 1134 | + * <p> |
| 1135 | + * Example: |
| 1136 | + * |
| 1137 | + * <pre>{@code |
| 1138 | + * Map<String, Object> book = new HashMap<>(); |
| 1139 | + * book.put("__name__", firestore.collection("books").document("newBook")); |
| 1140 | + * book.put("title", "New Book"); |
| 1141 | + * |
| 1142 | + * firestore.pipeline() |
| 1143 | + * .literals(book) |
| 1144 | + * .insert() |
| 1145 | + * .execute() |
| 1146 | + * .get(); |
| 1147 | + * }</pre> |
| 1148 | + * |
| 1149 | + * @return A new {@code Pipeline} object with this stage appended to the stage |
| 1150 | + * list. |
1080 | 1151 | */ |
1081 | 1152 | @BetaApi |
1082 | 1153 | public Pipeline insert() { |
1083 | 1154 | return append(new Insert()); |
1084 | 1155 | } |
1085 | 1156 |
|
1086 | 1157 | /** |
1087 | | - * Performs an insert operation using documents from previous stages. |
| 1158 | + * Performs an insert operation using documents from previous stages into a |
| 1159 | + * specified target |
| 1160 | + * collection. |
| 1161 | + * |
| 1162 | + * <p> |
| 1163 | + * If documents have an ID (or expression evaluation for ID), they will use it. |
| 1164 | + * Otherwise, |
| 1165 | + * auto-generated IDs will be used if applicable (depending on the source). |
| 1166 | + * |
| 1167 | + * <p> |
| 1168 | + * Example: |
| 1169 | + * |
| 1170 | + * <pre>{@code |
| 1171 | + * CollectionReference backupCol = firestore.collection("books_backup"); |
| 1172 | + * |
| 1173 | + * Map<String, Object> book = new HashMap<>(); |
| 1174 | + * book.put("title", "New Book"); |
| 1175 | + * |
| 1176 | + * firestore.pipeline() |
| 1177 | + * .literals(book) |
| 1178 | + * .insert(backupCol) |
| 1179 | + * .execute() |
| 1180 | + * .get(); |
| 1181 | + * }</pre> |
1088 | 1182 | * |
1089 | 1183 | * @param target The collection to insert to. |
1090 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
| 1184 | + * @return A new {@code Pipeline} object with this stage appended to the stage |
| 1185 | + * list. |
1091 | 1186 | */ |
1092 | 1187 | @BetaApi |
1093 | 1188 | public Pipeline insert(CollectionReference target) { |
1094 | 1189 | return append(new Insert().withCollection(target)); |
1095 | 1190 | } |
1096 | 1191 |
|
1097 | 1192 | /** |
1098 | | - * Performs an insert operation using documents from previous stages. |
| 1193 | + * Performs an insert operation using an {@link Insert} stage. |
| 1194 | + * |
| 1195 | + * <p> |
| 1196 | + * This method allows you to use a pre-configured {@link Insert} stage. |
| 1197 | + * |
| 1198 | + * <p> |
| 1199 | + * Example: Use a pre-configured {@link Insert} stage with a target collection |
| 1200 | + * and ID expression, reading from literals. |
| 1201 | + * |
| 1202 | + * <pre>{@code |
| 1203 | + * CollectionReference targetCol = firestore.collection("books_backup"); |
| 1204 | + * Insert insertStage = new Insert().withCollection(targetCol).withIdExpression(field("custom_id")); |
| 1205 | + * |
| 1206 | + * Map<String, Object> book = new HashMap<>(); |
| 1207 | + * book.put("custom_id", "book1"); |
| 1208 | + * book.put("title", "Book 1"); |
| 1209 | + * |
| 1210 | + * firestore.pipeline() |
| 1211 | + * .literals(book) |
| 1212 | + * .insert(insertStage) |
| 1213 | + * .execute() |
| 1214 | + * .get(); |
| 1215 | + * }</pre> |
1099 | 1216 | * |
1100 | 1217 | * @param insertStage The {@code Insert} stage to append. |
1101 | | - * @return A new {@code Pipeline} object with this stage appended to the stage list. |
| 1218 | + * @return A new {@code Pipeline} object with this stage appended to the stage |
| 1219 | + * list. |
1102 | 1220 | */ |
1103 | | - @InternalApi |
| 1221 | + @BetaApi |
1104 | 1222 | public Pipeline insert(Insert insertStage) { |
1105 | 1223 | return append(insertStage); |
1106 | 1224 | } |
|
0 commit comments