Status ModifierObjectReplace::init(const BSONElement& modExpr, const Options& opts) {

        if (modExpr.type() != Object) {
            // Impossible, really since the caller check this already...
            return Status(ErrorCodes::BadValue,
                          str::stream() << "object replace expects full object but type was "
                                        << modExpr.type());
        }

        if (opts.enforceOkForStorage) {
            Status s = modExpr.embeddedObject().storageValid();
            if (!s.isOK())
                return s;
        } else {
            // storageValid checks for $-prefixed field names, so only run if we didn't do that.
            BSONObjIterator it(modExpr.embeddedObject());
            while (it.more()) {
                BSONElement elem = it.next();
                if (*elem.fieldName() == '$') {
                    return Status(ErrorCodes::BadValue,
                                  str::stream() << "A replace document can't"
                                                   " contain update modifiers: "
                                                << elem.fieldNameStringData());
                }
            }
        }

        // We make a copy of the object here because the update driver does not guarantees, in
        // the case of object replacement, that the modExpr is going to outlive this mod.
        _val = modExpr.embeddedObject().getOwned();
        fixupTimestamps(_val);

        return Status::OK();
    }
Esempio n. 2
0
    Status ModifierObjectReplace::init(const BSONElement& modExpr, const Options& opts) {

        if (modExpr.type() != Object) {
            return Status(ErrorCodes::BadValue, mongoutils::str::stream() <<
                          "object replace expects full object but type was " << modExpr.type());
        }

        if (opts.enforceOkForStorage && !modExpr.embeddedObject().okForStorageAsRoot()) {
            return Status(ErrorCodes::BadValue, "not okForStorage: "
                                                " the document has invalid fields");
        }

        BSONObjIterator it(modExpr.embeddedObject());
        while (it.more()) {
            BSONElement elem = it.next();
            if (*elem.fieldName() == '$') {
                return Status(ErrorCodes::BadValue, "can't mix modifiers and non-modifiers");
            }
        }

        // We make a copy of the object here because the update driver does not guarantees, in
        // the case of object replacement, that the modExpr is going to outlive this mod.
        _val = modExpr.embeddedObject().getOwned();
        fixupTimestamps(_val);

        return Status::OK();
    }
Status ModifierObjectReplace::init(const BSONElement& modExpr,
                                   const Options& opts,
                                   bool* positional) {
    if (modExpr.type() != Object) {
        // Impossible, really since the caller check this already...
        return Status(ErrorCodes::BadValue,
                      str::stream() << "Document replacement expects a complete document"
                                       " but the type supplied was " << modExpr.type());
    }

    // Object replacements never have positional operator.
    if (positional)
        *positional = false;

    // We make a copy of the object here because the update driver does not guarantees, in
    // the case of object replacement, that the modExpr is going to outlive this mod.
    _val = modExpr.embeddedObject().getOwned();
    return fixupTimestamps(_val);
}