|
36 | 36 | import org.springframework.data.mongodb.core.query.Query;
|
37 | 37 | import org.springframework.data.mongodb.core.query.Update;
|
38 | 38 |
|
| 39 | +import java.time.Instant; |
39 | 40 | import java.util.Set;
|
40 | 41 |
|
41 | 42 | import static org.lowcoder.domain.util.QueryDslUtils.fieldName;
|
@@ -243,6 +244,30 @@ public void processDocument(Document document) {
|
243 | 244 | });
|
244 | 245 | }
|
245 | 246 |
|
| 247 | + @ChangeSet(order = "024", id = "fill-create-at", author = "") |
| 248 | + public void fillCreateAt(MongockTemplate mongoTemplate) { |
| 249 | + // Create a query to match all documents |
| 250 | + Query query = new Query(); |
| 251 | + |
| 252 | + // Use a DocumentCallbackHandler to iterate through each document |
| 253 | + mongoTemplate.executeQuery(query, "folder", new DocumentCallbackHandler() { |
| 254 | + @Override |
| 255 | + public void processDocument(Document document) { |
| 256 | + Object object = document.get("createdAt"); |
| 257 | + if(object != null) return; |
| 258 | + // Create an update object to add the 'gid' field |
| 259 | + Update update = new Update(); |
| 260 | + update.set("createdAt", Instant.now()); |
| 261 | + |
| 262 | + // Create a query to match the current document by its _id |
| 263 | + Query idQuery = new Query(Criteria.where("_id").is(document.getObjectId("_id"))); |
| 264 | + |
| 265 | + // Update the document with the new 'gid' field |
| 266 | + mongoTemplate.updateFirst(idQuery, update, "folder"); |
| 267 | + } |
| 268 | + }); |
| 269 | + } |
| 270 | + |
246 | 271 | private void addGidField(MongockTemplate mongoTemplate, String collectionName) {
|
247 | 272 | // Create a query to match all documents
|
248 | 273 | Query query = new Query();
|
|
0 commit comments