private Object getSubdocumentValue(Object document, String key, AtomicReference<Integer> matchPos)
     throws MongoServerException {
   int dotPos = key.indexOf('.');
   if (dotPos > 0) {
     String mainKey = key.substring(0, dotPos);
     String subKey = getSubkey(key, dotPos, matchPos);
     Object subObject = Utils.getListSafe(document, mainKey);
     if (subObject instanceof BSONObject || subObject instanceof List<?>) {
       return getSubdocumentValue(subObject, subKey, matchPos);
     } else {
       return null;
     }
   } else {
     return Utils.getListSafe(document, key);
   }
 }
  private void changeSubdocumentValue(
      Object document, String key, Object newValue, AtomicReference<Integer> matchPos)
      throws MongoServerException {
    int dotPos = key.indexOf('.');
    if (dotPos > 0) {
      String mainKey = key.substring(0, dotPos);
      String subKey = getSubkey(key, dotPos, matchPos);

      Object subObject = Utils.getListSafe(document, mainKey);
      if (subObject instanceof BSONObject || subObject instanceof List<?>) {
        changeSubdocumentValue(subObject, subKey, newValue, matchPos);
      } else {
        BSONObject obj = new BasicBSONObject();
        changeSubdocumentValue(obj, subKey, newValue, matchPos);
        Utils.setListSafe(document, mainKey, obj);
      }
    } else {
      Utils.setListSafe(document, key, newValue);
    }
  }