Skip to content

Commit

Permalink
set the datastore value before doing document conversions
Browse files Browse the repository at this point in the history
fixes #2472
  • Loading branch information
evanchooly committed Aug 23, 2023
1 parent de73195 commit c4d8c0b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/dev/morphia/query/Modify.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.bson.Document;

import static dev.morphia.internal.DatastoreHolder.holder;

/**
* Represents a modify operation
*
Expand Down Expand Up @@ -50,6 +52,7 @@ public T execute() {
*/
@Nullable
public T execute(ModifyOptions options) {
holder.set(getDatastore());
MongoCollection<T> collection = getDatastore().configureCollection(options, getCollection());
Document update = toDocument();

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/dev/morphia/query/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import org.bson.Document;

import static dev.morphia.internal.DatastoreHolder.holder;

/**
* Defines an update operation
*
Expand Down Expand Up @@ -47,6 +49,8 @@ public UpdateResult execute() {
* @return the results
*/
public UpdateResult execute(UpdateOptions options) {
holder.set(getDatastore());

Document updateOperations = toDocument();
final Document queryObject = getQuery().toDocument();
if (options.isUpsert()) {
Expand Down
25 changes: 20 additions & 5 deletions core/src/test/java/dev/morphia/test/TestUpdateOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import dev.morphia.query.updates.UpdateOperator;
import dev.morphia.test.models.Book;
import dev.morphia.test.models.Circle;
import dev.morphia.test.models.FacebookUser;
import dev.morphia.test.models.Hotel;
import dev.morphia.test.models.Rectangle;
import dev.morphia.test.models.Shape;
Expand Down Expand Up @@ -102,7 +103,9 @@
import static dev.morphia.query.updates.UpdateOperators.unset;
import static dev.morphia.query.updates.UpdateOperators.xor;
import static java.lang.String.format;
import static java.time.LocalDate.now;
import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.MONTHS;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -148,6 +151,17 @@ public void retainsClassName() {
.next();
}

@Test(description = "see https://github.com/MorphiaOrg/morphia/issues/2472 for details")
public void testUpdateWithDocumentConversion() {
getDs().find(Hotel.class).filter(eq("_id", ObjectId.get()))
.disableValidation()
.update(
set("last_updated", LocalDateTime.now()),
push("logs", List.of(Map.of("1", 1L))),
push("user_detail", List.of(new FacebookUser())))
.execute();
}

@Test
public void shouldUpdateAnArrayElement() {
// given
Expand Down Expand Up @@ -551,7 +565,7 @@ public void testMaxWithDates() {

getDs().save(entities);
UpdateResult updated = getDs().find(User.class)
.update(max("joined", LocalDate.now()))
.update(max("joined", now()))
.execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);

Expand All @@ -575,10 +589,11 @@ public void testMaxWithDates() {

@Test
public void testMinWithDates() {
getDs().find(User.class).delete(new DeleteOptions().multi(true));
List<User> entities = List.of(
new User("User 1", LocalDate.of(2003, 7, 13)),
new User("User 2", LocalDate.of(2009, 12, 1)),
new User("User 3", LocalDate.of(2015, 8, 19)));
new User("User 1", now().minus(1, MONTHS)),
new User("User 2", now().minus(2, MONTHS)),
new User("User 3", now().minus(3, MONTHS)));

getDs().save(entities);
UpdateResult updated = getDs().find(User.class)
Expand All @@ -589,7 +604,7 @@ public void testMinWithDates() {
getDs().find(User.class).delete();
getDs().save(entities);
updated = getDs().find(User.class)
.update(min("joined", Instant.now().minus(5000, DAYS)))
.update(min("joined", Instant.now().minus(70, DAYS)))
.execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 2);

Expand Down

0 comments on commit c4d8c0b

Please sign in to comment.