|
| 1 | +package com.baeldung.mongo.crud; |
| 2 | + |
| 3 | +import com.baeldung.mongo.crud.model.Event; |
| 4 | +import com.mongodb.BasicDBObject; |
| 5 | +import com.mongodb.BasicDBObjectBuilder; |
| 6 | +import com.mongodb.MongoException; |
| 7 | +import com.mongodb.client.MongoClient; |
| 8 | +import com.mongodb.client.MongoClients; |
| 9 | +import com.mongodb.client.MongoCollection; |
| 10 | +import com.mongodb.client.MongoDatabase; |
| 11 | +import com.mongodb.client.model.UpdateOptions; |
| 12 | +import com.mongodb.client.model.Updates; |
| 13 | +import com.mongodb.client.result.InsertOneResult; |
| 14 | +import com.mongodb.client.result.DeleteResult; |
| 15 | +import com.mongodb.client.result.UpdateResult; |
| 16 | +import org.bson.BsonValue; |
| 17 | +import org.bson.Document; |
| 18 | +import org.bson.codecs.configuration.CodecProvider; |
| 19 | +import org.bson.codecs.configuration.CodecRegistry; |
| 20 | +import org.bson.codecs.pojo.PojoCodecProvider; |
| 21 | +import org.bson.conversions.Bson; |
| 22 | + |
| 23 | +import java.time.LocalDate; |
| 24 | +import java.time.LocalDateTime; |
| 25 | +import java.time.OffsetDateTime; |
| 26 | +import java.time.ZoneId; |
| 27 | +import java.time.ZoneOffset; |
| 28 | +import java.time.ZonedDateTime; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static com.mongodb.client.model.Filters.and; |
| 33 | +import static com.mongodb.client.model.Filters.eq; |
| 34 | +import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry; |
| 35 | +import static com.mongodb.client.model.Filters.gte; |
| 36 | +import static com.mongodb.client.model.Filters.lt; |
| 37 | +import static org.bson.codecs.configuration.CodecRegistries.fromProviders; |
| 38 | +import static org.bson.codecs.configuration.CodecRegistries.fromRegistries; |
| 39 | + |
| 40 | +public class CrudClient { |
| 41 | + |
| 42 | + private static String uri = "mongodb://localhost:27017"; |
| 43 | + private static MongoClient mongoClient = MongoClients.create(uri); |
| 44 | + private static MongoDatabase db; |
| 45 | + private static MongoCollection<Event> collection; |
| 46 | + |
| 47 | + public static OffsetDateTime offsetDateTime = OffsetDateTime.of(LocalDateTime.of(2022, 6, 4, 11, 0, 0), |
| 48 | + ZoneOffset.ofHours(2)); |
| 49 | + |
| 50 | + public static Event pianoLessons = new Event( |
| 51 | + "Piano lessons", |
| 52 | + "Foo Bvld", |
| 53 | + LocalDateTime.of(2022, 6, 4, 11, 0, 0)); |
| 54 | + |
| 55 | + public static Event soccerGame = new Event( |
| 56 | + "Soccer game", |
| 57 | + "Bar Avenue", |
| 58 | + LocalDateTime.of(2022, 6, 10, 17, 0, 0)); |
| 59 | + |
| 60 | + public static Event pianoLessonsTZ = new Event( |
| 61 | + "Piano lessons", |
| 62 | + "Baz Bvld", |
| 63 | + LocalDateTime.of(2022, 12, 31, 12, 0, 0), |
| 64 | + ZoneOffset.ofHours(2).toString()); |
| 65 | + |
| 66 | + public static LocalDateTime dateQuery = LocalDateTime.of(2022, 6, 10, 17, 0, 0); |
| 67 | + public static LocalDateTime dateQueryEventWithTZ = LocalDateTime.of(2022, 12, 31, 12, 0, 0); |
| 68 | + |
| 69 | + public static LocalDateTime from = LocalDateTime.of(2022, 06, 04, 12, 0, 0); |
| 70 | + public static LocalDateTime to = LocalDateTime.of(2022, 06, 10, 17, 0, 0); |
| 71 | + |
| 72 | + public static LocalDate updateManyFrom = LocalDate.of(2022, 1, 1); |
| 73 | + public static LocalDate updateManyTo = LocalDate.of(2023, 1, 1); |
| 74 | + |
| 75 | + public static LocalDate deleteFrom = LocalDate.of(2022, 1, 1); |
| 76 | + public static LocalDate deleteTo = LocalDate.of(2023, 01, 01); |
| 77 | + |
| 78 | + public static void setup() { |
| 79 | + CodecProvider codecProvider = PojoCodecProvider.builder().automatic(true).build(); |
| 80 | + CodecRegistry codecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(codecProvider)); |
| 81 | + |
| 82 | + db = mongoClient.getDatabase("calendar").withCodecRegistry(codecRegistry); |
| 83 | + collection = db.getCollection("my_events", Event.class); |
| 84 | + } |
| 85 | + |
| 86 | + public static void close() { |
| 87 | + mongoClient.close(); |
| 88 | + } |
| 89 | + |
| 90 | + public static BsonValue insertEventsWithDate(Event e) { |
| 91 | + try { |
| 92 | + InsertOneResult insertResult = collection.insertOne(e); |
| 93 | + return insertResult.getInsertedId(); |
| 94 | + } catch (MongoException me) { |
| 95 | + System.err.println("Failed to insert with error: " + me); |
| 96 | + throw me; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static Event readEventsByDate(LocalDateTime localDateTime) { |
| 101 | + try { |
| 102 | + Event event = collection |
| 103 | + .find(eq("dateTime", localDateTime)) |
| 104 | + .first(); |
| 105 | + return event; |
| 106 | + } catch (MongoException me) { |
| 107 | + System.err.println("Failed to read with error: " + me); |
| 108 | + throw me; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public static List<Event> readEventsByDateRange(LocalDateTime from, LocalDateTime to) { |
| 113 | + BasicDBObject object = new BasicDBObject(); |
| 114 | + object.put("dateTime", BasicDBObjectBuilder |
| 115 | + .start("$gte", from) |
| 116 | + .add("$lte", to) |
| 117 | + .get()); |
| 118 | + try { |
| 119 | + List<Event> list = new ArrayList<Event>(collection.find(object).into(new ArrayList<Event>())); |
| 120 | + return list; |
| 121 | + } catch (MongoException me) { |
| 122 | + System.err.println("Failed to read with error: " + me); |
| 123 | + throw me; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static LocalDateTime readEventsByDateWithTZ(LocalDateTime localDateTime) { |
| 128 | + try { |
| 129 | + Event event = collection |
| 130 | + .find(eq("dateTime", localDateTime)) |
| 131 | + .first(); |
| 132 | + OffsetDateTime offsetDateTime = OffsetDateTime.of(event.dateTime, ZoneOffset.of(pianoLessonsTZ.timeZoneOffset)); |
| 133 | + ZonedDateTime zoned = offsetDateTime.atZoneSameInstant(ZoneId.of("America/Toronto")); |
| 134 | + return zoned.toLocalDateTime(); |
| 135 | + } catch (MongoException me) { |
| 136 | + System.err.println("Failed to read with error: " + me); |
| 137 | + throw me; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public static long updateDateField() { |
| 142 | + Document document = new Document().append("title", "Piano lessons"); |
| 143 | + Bson update = Updates.currentDate("updatedAt"); |
| 144 | + UpdateOptions options = new UpdateOptions().upsert(false); |
| 145 | + try { |
| 146 | + UpdateResult result = collection.updateOne(document, update, options); |
| 147 | + return result.getModifiedCount(); |
| 148 | + } catch (MongoException me) { |
| 149 | + System.err.println("Failed to update with error: " + me); |
| 150 | + throw me; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public static long updateManyEventsWithDateCriteria(LocalDate updateManyFrom, LocalDate updateManyTo) { |
| 155 | + Bson query = and(gte("dateTime", updateManyFrom), lt("dateTime", updateManyTo)); |
| 156 | + Bson updates = Updates.currentDate("dateTime"); |
| 157 | + try { |
| 158 | + UpdateResult result = collection.updateMany(query, updates); |
| 159 | + return result.getModifiedCount(); |
| 160 | + } catch(MongoException me) { |
| 161 | + System.err.println("Failed to replace/update with error: " + me); |
| 162 | + throw me; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + public static long deleteEventsByDate(LocalDate from, LocalDate to) { |
| 167 | + Bson query = and(gte("dateTime", from), lt("dateTime", to)); |
| 168 | + try { |
| 169 | + DeleteResult result = collection.deleteMany(query); |
| 170 | + return result.getDeletedCount(); |
| 171 | + } catch (MongoException me) { |
| 172 | + System.err.println("Failed to delete with error: " + me); |
| 173 | + throw me; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public static void dropDb() { |
| 178 | + db.drop(); |
| 179 | + } |
| 180 | + |
| 181 | + public static void main(String[] args) { |
| 182 | + } |
| 183 | +} |
0 commit comments