Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

getLastError has _id of of upserted object SERVER-1173

last error cleaning
  • Loading branch information...
commit 1026c72507fb6eb7d2bc16d479d2ec86ac7032cd 1 parent f8901be
@erh erh authored
View
4 db/instance.cpp
@@ -453,7 +453,7 @@ namespace mongo {
Client::Context ctx( ns );
UpdateResult res = updateObjects(ns, toupdate, query, upsert, multi, true, op.debug() );
- recordUpdate( res.existing , (int) res.num ); // for getlasterror
+ lastError.getSafe()->recordUpdate( res.existing , res.num , res.upserted ); // for getlasterror
}
void receivedDelete(Message& m, CurOp& op) {
@@ -475,7 +475,7 @@ namespace mongo {
Client::Context ctx(ns);
long long n = deleteObjects(ns, pattern, justOne, true);
- recordDelete( (int) n );
+ lastError.getSafe()->recordDelete( n );
}
QueryResult* emptyMoreResult(long long);
View
2  db/lasterror.cpp
@@ -57,6 +57,8 @@ namespace mongo {
b.append( "code" , code );
if ( updatedExisting != NotUpdate )
b.appendBool( "updatedExisting", updatedExisting == True );
+ if ( upsertedId.isSet() )
+ b.append( "upserted" , upsertedId );
b.appendNumber( "n", nObjects );
}
View
31 db/lasterror.h
@@ -17,6 +17,8 @@
#pragma once
+#include "../bson/oid.h"
+
namespace mongo {
class BSONObjBuilder;
class Message;
@@ -25,7 +27,7 @@ namespace mongo {
int code;
string msg;
enum UpdatedExistingType { NotUpdate, True, False } updatedExisting;
- /* todo: nObjects should be 64 bit */
+ OID upsertedId;
long long nObjects;
int nPrev;
bool valid;
@@ -36,10 +38,13 @@ namespace mongo {
code = _code;
msg = _msg;
}
- void recordUpdate( bool _updatedExisting, long long nChanged ) {
+ void recordUpdate( bool _updateObjects , long long _nObjects , OID _upsertedId ){
reset( true );
- nObjects = nChanged;
- updatedExisting = _updatedExisting ? True : False;
+ nObjects = _nObjects;
+ updatedExisting = _updateObjects ? True : False;
+ if ( _upsertedId.isSet() )
+ upsertedId = _upsertedId;
+
}
void recordDelete( long long nDeleted ) {
reset( true );
@@ -57,6 +62,7 @@ namespace mongo {
nPrev = 1;
valid = _valid;
disabled = false;
+ upsertedId.clear();
}
void appendSelf( BSONObjBuilder &b );
static LastError noError;
@@ -67,6 +73,11 @@ namespace mongo {
LastErrorHolder() : _id( 0 ) {}
LastError * get( bool create = false );
+ LastError * getSafe(){
+ LastError * le = get(false);
+ assert( le );
+ return le;
+ }
LastError * _get( bool create = false ); // may return a disabled LastError
@@ -107,16 +118,4 @@ namespace mongo {
void raiseError(int code , const char *msg);
- inline void recordUpdate( bool updatedExisting, int nChanged ) {
- LastError *le = lastError.get();
- if ( le )
- le->recordUpdate( updatedExisting, nChanged );
- }
-
- inline void recordDelete( int nDeleted ) {
- LastError *le = lastError.get();
- if ( le )
- le->recordDelete( nDeleted );
- }
-
} // namespace mongo
View
22 db/query.h
@@ -82,24 +82,18 @@ namespace mongo {
bool existing;
bool mod;
long long num;
+ OID upserted;
- UpdateResult( bool e, bool m, unsigned long long n )
- : existing(e) , mod(m), num(n ){}
+ UpdateResult( bool e, bool m, unsigned long long n , const BSONObj& upsertedObject = BSONObj() )
+ : existing(e) , mod(m), num(n){
+ upserted.clear();
- int oldCode(){
- if ( ! num )
- return 0;
-
- if ( existing ){
- if ( mod )
- return 2;
- return 1;
+ BSONElement id = upsertedObject["_id"];
+ if ( ! e && n == 1 && id.type() == jstOID ){
+ upserted = id.OID();
}
-
- if ( mod )
- return 3;
- return 4;
}
+
};
/* returns true if an existing object was updated, false if no existing object was found.
View
7 db/update.cpp
@@ -978,11 +978,10 @@ namespace mongo {
if ( profile )
ss << " fastmodinsert ";
theDataFileMgr.insertWithObjMod(ns, newObj, god);
- if ( profile )
- ss << " fastmodinsert ";
if ( logop )
logOp( "i", ns, newObj );
- return UpdateResult( 0 , 1 , 1 );
+
+ return UpdateResult( 0 , 1 , 1 , newObj );
}
uassert( 10159 , "multi update only works with $ operators" , ! multi );
checkNoMods( updateobj );
@@ -992,7 +991,7 @@ namespace mongo {
theDataFileMgr.insertWithObjMod(ns, no, god);
if ( logop )
logOp( "i", ns, no );
- return UpdateResult( 0 , 0 , 1 );
+ return UpdateResult( 0 , 0 , 1 , no );
}
return UpdateResult( 0 , 0 , 0 );
}
View
14 jstests/upsert1.js
@@ -0,0 +1,14 @@
+
+t = db.upsert1;
+t.drop();
+
+t.update( { x : 1 } , { $inc : { y : 1 } } , true );
+l = db.getLastErrorCmd();
+assert( l.upserted , "A1" );
+assert.eq( l.upserted.str , t.findOne()._id.str , "A2" );
+
+t.update( { x : 2 } , { x : 2 , y : 3 } , true );
+l = db.getLastErrorCmd();
+assert( l.upserted , "B1" );
+assert.eq( l.upserted.str , t.findOne( { x : 2 } )._id.str , "B2" );
+assert.eq( 2 , t.find().count() , "B3" );
Please sign in to comment.
Something went wrong with that request. Please try again.