From 7ff10a8611161d71303106eeacc869f5f6a4e9e3 Mon Sep 17 00:00:00 2001 From: Matt Dale <9760375+matthewdale@users.noreply.github.com> Date: Mon, 11 Apr 2022 14:07:44 -0700 Subject: [PATCH] GODRIVER-2323 Support int64 for 'n' field in insert, update, and delete ops. (#905) --- mongo/bulk_write.go | 8 ++++---- mongo/collection.go | 6 +++--- x/mongo/driver/operation/delete.go | 6 +++--- x/mongo/driver/operation/insert.go | 6 +++--- x/mongo/driver/operation/update.go | 12 ++++++------ 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/mongo/bulk_write.go b/mongo/bulk_write.go index 0446e7f373..e748ced6a5 100644 --- a/mongo/bulk_write.go +++ b/mongo/bulk_write.go @@ -114,7 +114,7 @@ func (bw *bulkWrite) runBatch(ctx context.Context, batch bulkWriteBatch) (BulkWr batchErr.Labels = writeErr.Labels batchErr.WriteConcernError = convertDriverWriteConcernError(writeErr.WriteConcernError) } - batchRes.InsertedCount = int64(res.N) + batchRes.InsertedCount = res.N case *DeleteOneModel, *DeleteManyModel: res, err := bw.runDelete(ctx, batch) if err != nil { @@ -126,7 +126,7 @@ func (bw *bulkWrite) runBatch(ctx context.Context, batch bulkWriteBatch) (BulkWr batchErr.Labels = writeErr.Labels batchErr.WriteConcernError = convertDriverWriteConcernError(writeErr.WriteConcernError) } - batchRes.DeletedCount = int64(res.N) + batchRes.DeletedCount = res.N case *ReplaceOneModel, *UpdateOneModel, *UpdateManyModel: res, err := bw.runUpdate(ctx, batch) if err != nil { @@ -138,8 +138,8 @@ func (bw *bulkWrite) runBatch(ctx context.Context, batch bulkWriteBatch) (BulkWr batchErr.Labels = writeErr.Labels batchErr.WriteConcernError = convertDriverWriteConcernError(writeErr.WriteConcernError) } - batchRes.MatchedCount = int64(res.N) - batchRes.ModifiedCount = int64(res.NModified) + batchRes.MatchedCount = res.N + batchRes.ModifiedCount = res.NModified batchRes.UpsertedCount = int64(len(res.Upserted)) for _, upsert := range res.Upserted { batchRes.UpsertedIDs[int64(batch.indexes[upsert.Index])] = upsert.ID diff --git a/mongo/collection.go b/mongo/collection.go index 37c66764c5..590d928045 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -473,7 +473,7 @@ func (coll *Collection) delete(ctx context.Context, filter interface{}, deleteOn if rr&expectedRr == 0 { return nil, err } - return &DeleteResult{DeletedCount: int64(op.Result().N)}, err + return &DeleteResult{DeletedCount: op.Result().N}, err } // DeleteOne executes a delete command to delete at most one document from the collection. @@ -582,8 +582,8 @@ func (coll *Collection) updateOrReplace(ctx context.Context, filter bsoncore.Doc opRes := op.Result() res := &UpdateResult{ - MatchedCount: int64(opRes.N), - ModifiedCount: int64(opRes.NModified), + MatchedCount: opRes.N, + ModifiedCount: opRes.NModified, UpsertedCount: int64(len(opRes.Upserted)), } if len(opRes.Upserted) > 0 { diff --git a/x/mongo/driver/operation/delete.go b/x/mongo/driver/operation/delete.go index e485115403..beb893c717 100644 --- a/x/mongo/driver/operation/delete.go +++ b/x/mongo/driver/operation/delete.go @@ -42,7 +42,7 @@ type Delete struct { // DeleteResult represents a delete result returned by the server. type DeleteResult struct { // Number of documents successfully deleted. - N int32 + N int64 } func buildDeleteResult(response bsoncore.Document) (DeleteResult, error) { @@ -55,9 +55,9 @@ func buildDeleteResult(response bsoncore.Document) (DeleteResult, error) { switch element.Key() { case "n": var ok bool - dr.N, ok = element.Value().AsInt32OK() + dr.N, ok = element.Value().AsInt64OK() if !ok { - return dr, fmt.Errorf("response field 'n' is type int32, but received BSON type %s", element.Value().Type) + return dr, fmt.Errorf("response field 'n' is type int32 or int64, but received BSON type %s", element.Value().Type) } } } diff --git a/x/mongo/driver/operation/insert.go b/x/mongo/driver/operation/insert.go index f66cfbee1f..993eac1013 100644 --- a/x/mongo/driver/operation/insert.go +++ b/x/mongo/driver/operation/insert.go @@ -41,7 +41,7 @@ type Insert struct { // InsertResult represents an insert result returned by the server. type InsertResult struct { // Number of documents successfully inserted. - N int32 + N int64 } func buildInsertResult(response bsoncore.Document) (InsertResult, error) { @@ -54,9 +54,9 @@ func buildInsertResult(response bsoncore.Document) (InsertResult, error) { switch element.Key() { case "n": var ok bool - ir.N, ok = element.Value().AsInt32OK() + ir.N, ok = element.Value().AsInt64OK() if !ok { - return ir, fmt.Errorf("response field 'n' is type int32, but received BSON type %s", element.Value().Type) + return ir, fmt.Errorf("response field 'n' is type int32 or int64, but received BSON type %s", element.Value().Type) } } } diff --git a/x/mongo/driver/operation/update.go b/x/mongo/driver/operation/update.go index 5a27fa95a0..3cd11848d8 100644 --- a/x/mongo/driver/operation/update.go +++ b/x/mongo/driver/operation/update.go @@ -51,9 +51,9 @@ type Upsert struct { // UpdateResult contains information for the result of an Update operation. type UpdateResult struct { // Number of documents matched. - N int32 + N int64 // Number of documents modified. - NModified int32 + NModified int64 // Information about upserted documents. Upserted []Upsert } @@ -68,15 +68,15 @@ func buildUpdateResult(response bsoncore.Document) (UpdateResult, error) { switch element.Key() { case "nModified": var ok bool - ur.NModified, ok = element.Value().Int32OK() + ur.NModified, ok = element.Value().AsInt64OK() if !ok { - return ur, fmt.Errorf("response field 'nModified' is type int32, but received BSON type %s", element.Value().Type) + return ur, fmt.Errorf("response field 'nModified' is type int32 or int64, but received BSON type %s", element.Value().Type) } case "n": var ok bool - ur.N, ok = element.Value().Int32OK() + ur.N, ok = element.Value().AsInt64OK() if !ok { - return ur, fmt.Errorf("response field 'n' is type int32, but received BSON type %s", element.Value().Type) + return ur, fmt.Errorf("response field 'n' is type int32 or int64, but received BSON type %s", element.Value().Type) } case "upserted": arr, ok := element.Value().ArrayOK()