diff options
author | Eliot Horowitz <eliot@10gen.com> | 2011-03-23 10:56:19 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2011-03-23 10:56:19 -0400 |
commit | c3667000b35c66b24ca8f430215d891b505846ca (patch) | |
tree | a56c44078a292a98632202dc55ad0c00335336af /db/dbcommands.cpp | |
parent | d2bb5fa4f3145c57fb76cc39a7c742ca82347810 (diff) | |
download | mongo-c3667000b35c66b24ca8f430215d891b505846ca.tar.gz |
moved findAndModify to its own file
Diffstat (limited to 'db/dbcommands.cpp')
-rw-r--r-- | db/dbcommands.cpp | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/db/dbcommands.cpp b/db/dbcommands.cpp index b92ed946b12..c0d405e8bfa 100644 --- a/db/dbcommands.cpp +++ b/db/dbcommands.cpp @@ -1414,116 +1414,6 @@ namespace mongo { } } cmdConvertToCapped; - /* Find and Modify an object returning either the old (default) or new value*/ - class CmdFindAndModify : public Command { - public: - virtual void help( stringstream &help ) const { - help << - "{ findAndModify: \"collection\", query: {processed:false}, update: {$set: {processed:true}}, new: true}\n" - "{ findAndModify: \"collection\", query: {processed:false}, remove: true, sort: {priority:-1}}\n" - "Either update or remove is required, all other fields have default values.\n" - "Output is in the \"value\" field\n"; - } - - CmdFindAndModify() : Command("findAndModify", false, "findandmodify") { } - virtual bool logTheOp() { - return false; // the modification will be logged directly - } - virtual bool slaveOk() const { - return false; - } - virtual LockType locktype() const { return WRITE; } - virtual bool run(const string& dbname, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) { - static DBDirectClient db; - - string ns = dbname + '.' + cmdObj.firstElement().valuestr(); - - BSONObj origQuery = cmdObj.getObjectField("query"); // defaults to {} - Query q (origQuery); - BSONElement sort = cmdObj["sort"]; - if (!sort.eoo()) - q.sort(sort.embeddedObjectUserCheck()); - - bool upsert = cmdObj["upsert"].trueValue(); - - BSONObj fieldsHolder (cmdObj.getObjectField("fields")); - const BSONObj* fields = (fieldsHolder.isEmpty() ? NULL : &fieldsHolder); - - BSONObj out = db.findOne(ns, q, fields); - if (out.isEmpty()) { - if (!upsert) { - errmsg = "No matching object found"; - return false; - } - - BSONElement update = cmdObj["update"]; - uassert(13329, "upsert mode requires update field", !update.eoo()); - uassert(13330, "upsert mode requires query field", !origQuery.isEmpty()); - db.update(ns, origQuery, update.embeddedObjectUserCheck(), true); - - BSONObj gle = db.getLastErrorDetailed(); - if (gle["err"].type() == String) { - errmsg = gle["err"].String(); - return false; - } - - if (cmdObj["new"].trueValue()) { - BSONElement _id = gle["upserted"]; - if (_id.eoo()) - _id = origQuery["_id"]; - - out = db.findOne(ns, QUERY("_id" << _id), fields); - } - - } - else { - - if (cmdObj["remove"].trueValue()) { - uassert(12515, "can't remove and update", cmdObj["update"].eoo()); - db.remove(ns, QUERY("_id" << out["_id"]), 1); - - } - else { // update - - BSONElement queryId = origQuery["_id"]; - if (queryId.eoo() || getGtLtOp(queryId) != BSONObj::Equality) { - // need to include original query for $ positional operator - - BSONObjBuilder b; - b.append(out["_id"]); - BSONObjIterator it(origQuery); - while (it.more()) { - BSONElement e = it.next(); - if (strcmp(e.fieldName(), "_id")) - b.append(e); - } - q = Query(b.obj()); - } - - if (q.isComplex()) // update doesn't work with complex queries - q = Query(q.getFilter().getOwned()); - - BSONElement update = cmdObj["update"]; - uassert(12516, "must specify remove or update", !update.eoo()); - db.update(ns, q, update.embeddedObjectUserCheck()); - - BSONObj gle = db.getLastErrorDetailed(); - if (gle["err"].type() == String) { - errmsg = gle["err"].String(); - return false; - } - - if (cmdObj["new"].trueValue()) - out = db.findOne(ns, QUERY("_id" << out["_id"]), fields); - } - } - - result.append("value", out); - - return true; - } - } cmdFindAndModify; - /* Returns client's uri */ class CmdWhatsMyUri : public Command { public: |