docs: function (doc, keyparts, options) {
    options = options || {};
    var usedArrayIndex = false;
    for (var i = 0; i < keyparts.length; i++) {
      var last = (i === keyparts.length - 1);
      var keypart = keyparts[i];
      var indexable = isIndexable(doc);
      if (!indexable) {
        if (options.noCreate)
          return undefined;
        var e = MinimongoError(
          "cannot use the part '" + keypart + "' to traverse " + doc);
        e.setPropertyError = true;
        throw e;
      }
      if (_.isArray(doc)) {
        if (options.forbidArray)
          return null;
        if (keypart === '$') {
          if (usedArrayIndex)
            throw MinimongoError("Too many positional (i.e. '$') elements");
          if (!options.arrayIndices || !options.arrayIndices.length) {
            throw MinimongoError("The positional operator did not find the " +
                                 "match needed from the query");
          }
          keypart = options.arrayIndices[0];
          usedArrayIndex = true;
        } else if (helpers.isNumericKey(keypart)) {
          keypart = parseInt(keypart);
        } else {
          if (options.noCreate)
            return undefined;
          throw MinimongoError(
            "can't append to array using string field name ["
                      + keypart + "]");
        }
        if (last)
          // handle 'a.01'
          keyparts[i] = keypart;
        if (options.noCreate && keypart >= doc.length)
          return undefined;
        while (doc.length < keypart)
          doc.push(null);
        if (!last) {
          if (doc.length === keypart)
            doc.push({});
          else if (typeof doc[keypart] !== "object")
            throw MinimongoError("can't modify field '" + keyparts[i + 1] +
                        "' of list value " + JSON.stringify(doc[keypart]));
        }
      } else {
        if (keypart.length && keypart.substr(0, 1) === '$')
          throw MinimongoError("can't set field named " + keypart);
        if (!(keypart in doc)) {
          if (options.noCreate)
            return undefined;
          if (!last)
            doc[keypart] = {};
        }
      }
  
      if (last)
        return doc;
      doc = doc[keypart];
    }
  
    // notreached
  }