In this tutorial, we'll use the MongoDB Java Driver to execute date-related CRUD operations, such as creating and updating documents with date fields, and querying, updating, and deleting documents whose date fields fall within a given range. Show
2. SetupBefore diving into the implementation, let's set up our work environment. 2.1. Maven DependencyFirst, you should have MongoDB installed. If you don't, you can follow the official MongoDB installation guide to do so. Next, let's add the MongoDB Java Driver as a dependency to our pom.xml file:
2.2. POJO Data ModelLet's define a POJO to represent the documents contained in our database:
Note that we've declared two constructors. MongoDB uses the no-argument constructor by . The other constructor is for our own use throughout this tutorial. Let's also note that, while dateTime could have been a String variable, the best practice is to use date/time-specific JDK classes for date fields. Using String fields to represent dates requires extra effort to ensure the values are formatted correctly. We are now ready to connect a client to our database. 2.3. MongoDB ClientIn order for MongoDB to serialize/deserialize our Event POJO, we need to register PojoCodecProvider with MongoDB's CodecRegistry:
Let's create a database, collection, and client that will use the PojoCodecProvider we registered:
We're now ready to create documents and perform date-related CRUD operations. 3. Creating Documents With Date FieldsIn our POJO, we used LocalDateTime instead of a String in order to make it easier to work with date values. Let's take advantage of that now by constructing Event objects using LocalDateTime‘s convenient API:
We can insert the new Events into our database as follows:
Let's verify the insertions were successful by checking the id of the inserted documents:
4. Querying Documents Matching Date CriteriaNow that we have Events in our database, let's retrieve them based on their date fields. We can use the equality filter (eq) to retrieve documents matching a specific date and time:
Let's check the individual fields of the resulting Event:
We can also use the MongoDB BasicDBObject class along with the gte and lte operators to build more complex queries using Date ranges:
Since the soccer game is the only Event within the date range of our query, we should only see one Event object in the list, with the piano lesson excluded: 05. Updating DocumentsLet's explore two use cases for updating documents based on their date fields. First, we'll update the date field of a single document, and then we'll update multiple documents matching a date range. 5.1. Updating a Document's Date FieldTo update a MongoDB document, we can use the updateOne() method. Let's also use the currentDate() method to set the dateTime field of our piano lesson event: 1Note that the first argument to updateOne() is a Document object that MongoDB will use to match a single entry in our database. If multiple documents are a match, MongoDB will only update the first document it encounters. Let's also note that we passed false to the upsert() method. If we had instead passed in true, MongoDB would insert a new document if none of the existing documents matched. We can confirm the operation was successful by checking how many documents were modified: 25.2. Updating Documents Matching Date CriteriaTo update multiple documents, MongoDB provides the updateMany method. In this example, we'll update multiple events matching the date range from our query. Unlike updateOne(), the updateMany() method expects a second Bson object to encapsulate the query criteria that will define which documents we want to update. In this case, we'll specify a date range covering all events in 2022 by introducing the lt field operator: 3Just like with updateOne(), we can confirm this operation updated multiple events by checking the update count of our result object: 46. Deleting Documents Matching Date CriteriaAs with updates, we can delete one or multiple documents from our database at a time. Suppose we need to remove all events from 2022. Let's use a Bson date range query and the deleteMany() method to do that: 5Since all of the events we have created in this tutorial have a 2022 dateTime field value, deleteMany() removed them all from our collection. We can confirm this by checking the delete count: 67. Using Time ZonesMongoDB stores dates in UTC, and this cannot be changed. Thus, if we want our date fields to be specific to a time zone, we can store the time zone offset in a separate field and do the conversion ourselves. Let's add that field as a String: 7We'll need to adjust our constructor so we can set the new field when creating events: 8We can now create and insert events for specific time zones into our database. Let's use the ZoneOffset class to avoid having to manually format the time zone offset String: 9Note that since the offset is relative to UTC, the dateTime member variable must represent UTC time so we can correctly convert it later. Once we retrieve the document from the collection, we can make the conversion using the offset field and the OffsetDateTime class: 08. ConclusionIn this article, we learned how to perform date-related CRUD operations using Java and a MongoDB database. We used date values to create, retrieve, update, or remove documents in our database. Throughout our examples, we covered various helper classes and introduced MongoDB operators that are helpful when dealing with dates. Finally, to work around how MongoDB only stores dates in UTC, we learned how to work with date/time values that need to be time zone specific. |