Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed update embedded documents using $elemMatch #375

Open
mincong-h opened this issue Mar 29, 2020 · 0 comments · May be fixed by #376
Open

Failed update embedded documents using $elemMatch #375

mincong-h opened this issue Mar 29, 2020 · 0 comments · May be fixed by #376

Comments

@mincong-h
Copy link

Overview

Given the following document in collection:

{
   "name" : "Tenant 1",
   "appAllocations" : [
     { "appId" : "68e1cf5a-0253-4c4a-9bc1-1eb3f27d0902" , "maxUser" : 4 },
     { "appId" : "1bb05dc0-3acf-41a3-a5c2-551db93187fb" , "maxUser" : 42 }
  ]
}

When filtering it via $elemMatch and updating the entire embedded via positional operator $, the update failed:

"$set" : {
  "appAllocations.$" : {
    "appId" : "68e1cf5a-0253-4c4a-9bc1-1eb3f27d0902",
    "maxUser" : 9
  }
}

Then the update failed:

org.junit.ComparisonFailure: 
Expected :{ "name" : "Tenant 1" , "appAllocations" : [ { "appId" : "68e1cf5a-0253-4c4a-9bc1-1eb3f27d0902" , "maxUser" : 9} , { "appId" : "1bb05dc0-3acf-41a3-a5c2-551db93187fb" , "maxUser" : 42}]}
Actual   :{ "name" : "Tenant 1" , "appAllocations" : [ { "appId" : "68e1cf5a-0253-4c4a-9bc1-1eb3f27d0902" , "maxUser" : 4} , { "appId" : "1bb05dc0-3acf-41a3-a5c2-551db93187fb" , "maxUser" : 42}]}

However, this works on real MongoDB.

Workaround

Only update one field:

"$set" : {  "appAllocations.$.maxUser" : 9 }

Reproduction

@Test
public void testPositionalOperatorWithElemMatch2() {
  String random1 = UUID.randomUUID().toString();
  String random2 = UUID.randomUUID().toString();

  DBObject object = (DBObject) FongoJSON.parse("{ \"name\" : \"Tenant 1\", \"appAllocations\" : [ { \"appId\" : \""
      + random1 + "\" , \"maxUser\" : 4} , { \"appId\""
      + ": \"" + random2 + "\" , \"maxUser\" : 42}]}");
  DBObject query = (DBObject) FongoJSON.parse("{ \"appAllocations\" : { \"$elemMatch\" : { \"appId\" : \"" + random1 + "\"}}}");
  DBObject update = (DBObject) FongoJSON.parse("{ \"$set\" : { \"appAllocations.$\" : { \"appId\" : \"" + random1 + "\", \"maxUser\" : 9 }}}");
  System.out.println("update: " + update);

  DBObject expected = (DBObject) FongoJSON.parse("{ \"name\" : \"Tenant 1\", \"appAllocations\" : [ { \"appId\" : \""
      + random1 + "\" , \"maxUser\" : 9} , { \"appId\""
      + ": \"" + random2 + "\" , \"maxUser\" : 42}]}");
  UpdateEngine updateEngine = new UpdateEngine();

  assertEquals(expected.toString(),
      updateEngine.doUpdate(object, update, query, false).toString());
}
@mincong-h mincong-h linked a pull request Mar 29, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant