@Test public void one_$parent_unset() throws Exception { UpdateExpression expr = EvalTestContext.updateExpressionFromJson("{'$unset' : [ 'field2.$parent.field1', 'field2.$parent.field6.nf2', 'field2.$parent.field6.nf6.1','field2.$parent.field7.1'] }"); Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr); Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path())); Assert.assertNull(jsonDoc.get(new Path("field1"))); Assert.assertNull(jsonDoc.get(new Path("field6.nf2"))); Assert.assertEquals("three", jsonDoc.get(new Path("field6.nf6.1")).asText()); Assert.assertEquals(3, jsonDoc.get(new Path("field6.nf6#")).asInt()); Assert.assertEquals(3, jsonDoc.get(new Path("field6.nf6")).size()); Assert.assertEquals("elvalue2_1", jsonDoc.get(new Path("field7.1.elemf1")).asText()); Assert.assertEquals(3, jsonDoc.get(new Path("field7#")).asInt()); Assert.assertEquals(3, jsonDoc.get(new Path("field7")).size()); }
@Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; MutablePath p = new MutablePath(); for (AbsPath x : fields) { Path fld = new Path(contextPath, x.field); LOGGER.debug("Removing {}", fld); if (doc.modify(fld, null, false) != null) { ret = true; if (x.absArrayField != null) { p.set(x.absArrayField); p.rewriteIndexes(fld); ArrayNode node = (ArrayNode) doc.get(p); p.setLast(p.getLast() + "#"); doc.modify(p, factory.numberNode(node.size()), false); } } } return ret; }
UnsetExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; MutablePath p = new MutablePath(); for (AbsPath x : fields) { Path fld = new Path(contextPath, x.field); LOGGER.debug("Removing {}", fld); if (doc.modify(fld, null, false) != null) { ret = true; if (x.absArrayField != null) { p.set(x.absArrayField); p.rewriteIndexes(fld); ArrayNode node = (ArrayNode) doc.get(p); p.setLast(p.getLast() + "#"); doc.modify(p, factory.numberNode(node.size()), false); } } } return ret; } }
UnsetExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; MutablePath p = new MutablePath(); for (AbsPath x : fields) { Path fld = new Path(contextPath, x.field); LOGGER.debug("Removing {}", fld); if (doc.modify(fld, null, false) != null) { ret = true; if (x.absArrayField != null) { p.set(x.absArrayField); p.rewriteIndexes(fld); ArrayNode node = (ArrayNode) doc.get(p); p.setLast(p.getLast() + "#"); doc.modify(p, factory.numberNode(node.size()), false); } } } return ret; } UnsetExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, UnsetExpression expr); }
UnsetExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; MutablePath p = new MutablePath(); for (AbsPath x : fields) { Path fld = new Path(contextPath, x.field); LOGGER.debug("Removing {}", fld); if (doc.modify(fld, null, false) != null) { ret = true; if (x.absArrayField != null) { p.set(x.absArrayField); p.rewriteIndexes(fld); ArrayNode node = (ArrayNode) doc.get(p); p.setLast(p.getLast() + "#"); doc.modify(p, factory.numberNode(node.size()), false); } } } return ret; } UnsetExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, UnsetExpression expr); @Override void getUpdateFields(Set<Path> setFields); @Override boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath); }
UnsetExpressionEvaluator extends Updater { @Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; MutablePath p = new MutablePath(); for (AbsPath x : fields) { Path fld = new Path(contextPath, x.field); LOGGER.debug("Removing {}", fld); if (doc.modify(fld, null, false) != null) { ret = true; if (x.absArrayField != null) { p.set(x.absArrayField); p.rewriteIndexes(fld); ArrayNode node = (ArrayNode) doc.get(p); p.setLast(p.getLast() + "#"); doc.modify(p, factory.numberNode(node.size()), false); } } } return ret; } UnsetExpressionEvaluator(JsonNodeFactory factory, FieldTreeNode context, UnsetExpression expr); @Override void getUpdateFields(Set<Path> setFields); @Override boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath);  }