Skip to content

Commit 16df779

Browse files
committed
feat!(NODE-5241): return raw result by default in find and modify
1 parent 9ab7f56 commit 16df779

File tree

8 files changed

+122
-234
lines changed

8 files changed

+122
-234
lines changed

src/collection.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import type {
1919
OptionalUnlessRequiredId,
2020
TODO_NODE_3286,
2121
UpdateFilter,
22-
WithId,
2322
WithoutId
2423
} from './mongo_types';
24+
import { WithId } from './mongo_types';
2525
import type { AggregateOptions } from './operations/aggregate';
2626
import { BulkWriteOperation } from './operations/bulk_write';
2727
import type { IndexInformationOptions } from './operations/common_functions';
@@ -820,10 +820,18 @@ export class Collection<TSchema extends Document = Document> {
820820
* @param filter - The filter used to select the document to remove
821821
* @param options - Optional settings for the command
822822
*/
823+
async findOneAndDelete(
824+
filter: Filter<TSchema>,
825+
options?: FindOneAndDeleteOptions & { returnRawResult: false }
826+
): Promise<ModifyResult<TSchema>>;
827+
async findOneAndDelete(
828+
filter: Filter<TSchema>,
829+
options?: FindOneAndDeleteOptions & { returnRawResult: true | undefined }
830+
): Promise<WithId<TSchema> | null>;
823831
async findOneAndDelete(
824832
filter: Filter<TSchema>,
825833
options?: FindOneAndDeleteOptions
826-
): Promise<ModifyResult<TSchema> | TSchema | null> {
834+
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
827835
return executeOperation(
828836
this.client,
829837
new FindOneAndDeleteOperation(
@@ -841,11 +849,30 @@ export class Collection<TSchema extends Document = Document> {
841849
* @param replacement - The Document that replaces the matching document
842850
* @param options - Optional settings for the command
843851
*/
852+
async findOneAndReplace(
853+
filter: Filter<TSchema>,
854+
replacement: WithoutId<TSchema>,
855+
options: FindOneAndReplaceOptions & { returnRawResult: false }
856+
): Promise<ModifyResult<TSchema>>;
857+
async findOneAndReplace(
858+
filter: Filter<TSchema>,
859+
replacement: WithoutId<TSchema>,
860+
options: FindOneAndReplaceOptions & { returnRawResult: true }
861+
): Promise<WithId<TSchema>>;
862+
async findOneAndReplace(
863+
filter: Filter<TSchema>,
864+
replacement: WithoutId<TSchema>,
865+
options: FindOneAndReplaceOptions
866+
): Promise<WithId<TSchema>>;
867+
async findOneAndReplace(
868+
filter: Filter<TSchema>,
869+
replacement: WithoutId<TSchema>
870+
): Promise<WithId<TSchema>>;
844871
async findOneAndReplace(
845872
filter: Filter<TSchema>,
846873
replacement: WithoutId<TSchema>,
847874
options?: FindOneAndReplaceOptions
848-
): Promise<ModifyResult<TSchema> | TSchema | null> {
875+
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
849876
return executeOperation(
850877
this.client,
851878
new FindOneAndReplaceOperation(
@@ -867,8 +894,18 @@ export class Collection<TSchema extends Document = Document> {
867894
async findOneAndUpdate(
868895
filter: Filter<TSchema>,
869896
update: UpdateFilter<TSchema>,
870-
options?: FindOneAndUpdateOptions
871-
): Promise<ModifyResult<TSchema> | TSchema | null> {
897+
options?: FindOneAndReplaceOptions & { returnRawResult: false }
898+
): Promise<ModifyResult<TSchema>>;
899+
async findOneAndUpdate(
900+
filter: Filter<TSchema>,
901+
update: UpdateFilter<TSchema>,
902+
options?: FindOneAndReplaceOptions & { returnRawResult: true | undefined }
903+
): Promise<WithId<TSchema>>;
904+
async findOneAndUpdate(
905+
filter: Filter<TSchema>,
906+
update: UpdateFilter<TSchema>,
907+
options?: FindOneAndReplaceOptions
908+
): Promise<WithId<TSchema> | ModifyResult<TSchema> | null> {
872909
return executeOperation(
873910
this.client,
874911
new FindOneAndUpdateOperation(

test/integration/crud/find.test.js

Lines changed: 14 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -781,126 +781,6 @@ describe('Find', function () {
781781
}
782782
});
783783

784-
/**
785-
* Test findOneAndUpdate a document
786-
*/
787-
it('shouldCorrectlyfindOneAndUpdateDocument', {
788-
metadata: {
789-
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
790-
},
791-
792-
test: function (done) {
793-
var configuration = this.configuration;
794-
var db = client.db(configuration.db);
795-
db.dropCollection('test_find_and_modify_a_document_1')
796-
.catch(() => null)
797-
.finally(() => {
798-
db.createCollection('test_find_and_modify_a_document_1', function (err, collection) {
799-
expect(err).to.not.exist;
800-
801-
// Test return new document on change
802-
collection.insert({ a: 1, b: 2 }, configuration.writeConcernMax(), function (err) {
803-
expect(err).to.not.exist;
804-
805-
// Let's modify the document in place
806-
collection.findOneAndUpdate(
807-
{ a: 1 },
808-
{ $set: { b: 3 } },
809-
{ returnDocument: ReturnDocument.AFTER },
810-
function (err, updated_doc) {
811-
expect(err).to.not.exist;
812-
test.equal(1, updated_doc.value.a);
813-
test.equal(3, updated_doc.value.b);
814-
815-
// Test return old document on change
816-
collection.insert(
817-
{ a: 2, b: 2 },
818-
configuration.writeConcernMax(),
819-
function (err) {
820-
expect(err).to.not.exist;
821-
822-
// Let's modify the document in place
823-
collection.findOneAndUpdate(
824-
{ a: 2 },
825-
{ $set: { b: 3 } },
826-
configuration.writeConcernMax(),
827-
function (err, result) {
828-
expect(err).to.not.exist;
829-
test.equal(2, result.value.a);
830-
test.equal(2, result.value.b);
831-
832-
// Test remove object on change
833-
collection.insert(
834-
{ a: 3, b: 2 },
835-
configuration.writeConcernMax(),
836-
function (err) {
837-
expect(err).to.not.exist;
838-
// Let's modify the document in place
839-
collection.findOneAndUpdate(
840-
{ a: 3 },
841-
{ $set: { b: 3 } },
842-
{ remove: true },
843-
function (err, updated_doc) {
844-
expect(err).to.not.exist;
845-
test.equal(3, updated_doc.value.a);
846-
test.equal(2, updated_doc.value.b);
847-
848-
// Let's upsert!
849-
collection.findOneAndUpdate(
850-
{ a: 4 },
851-
{ $set: { b: 3 } },
852-
{ returnDocument: ReturnDocument.AFTER, upsert: true },
853-
function (err, updated_doc) {
854-
expect(err).to.not.exist;
855-
test.equal(4, updated_doc.value.a);
856-
test.equal(3, updated_doc.value.b);
857-
858-
// Test selecting a subset of fields
859-
collection.insert(
860-
{ a: 100, b: 101 },
861-
configuration.writeConcernMax(),
862-
function (err, r) {
863-
expect(err).to.not.exist;
864-
865-
collection.findOneAndUpdate(
866-
{ a: 100 },
867-
{ $set: { b: 5 } },
868-
{
869-
returnDocument: ReturnDocument.AFTER,
870-
projection: { b: 1 }
871-
},
872-
function (err, updated_doc) {
873-
expect(err).to.not.exist;
874-
test.equal(2, Object.keys(updated_doc.value).length);
875-
test.equal(
876-
r.insertedIds[0].toHexString(),
877-
updated_doc.value._id.toHexString()
878-
);
879-
test.equal(5, updated_doc.value.b);
880-
test.equal('undefined', typeof updated_doc.value.a);
881-
client.close(done);
882-
}
883-
);
884-
}
885-
);
886-
}
887-
);
888-
}
889-
);
890-
}
891-
);
892-
}
893-
);
894-
}
895-
);
896-
}
897-
);
898-
});
899-
});
900-
});
901-
}
902-
});
903-
904784
/**
905785
* Test findOneAndUpdate a document with fields
906786
*/
@@ -925,8 +805,8 @@ describe('Find', function () {
925805
{ $set: { b: 3 } },
926806
{ returnDocument: ReturnDocument.AFTER, projection: { a: 1 } },
927807
function (err, updated_doc) {
928-
test.equal(2, Object.keys(updated_doc.value).length);
929-
test.equal(1, updated_doc.value.a);
808+
test.equal(2, Object.keys(updated_doc).length);
809+
test.equal(1, updated_doc.a);
930810
client.close(done);
931811
}
932812
);
@@ -981,53 +861,6 @@ describe('Find', function () {
981861
}
982862
});
983863

984-
/**
985-
* Test findOneAndUpdate a document
986-
*/
987-
it('Should Correctly Handle findOneAndUpdate Duplicate Key Error', {
988-
metadata: {
989-
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
990-
},
991-
992-
test: function (done) {
993-
var configuration = this.configuration;
994-
var client = configuration.newClient(configuration.writeConcernMax(), { maxPoolSize: 1 });
995-
client.connect(function (err, client) {
996-
expect(err).to.not.exist;
997-
var db = client.db(configuration.db);
998-
db.createCollection('findOneAndUpdateDuplicateKeyError', function (err, collection) {
999-
expect(err).to.not.exist;
1000-
collection.createIndex(
1001-
['name', 1],
1002-
{ unique: true, writeConcern: { w: 1 } },
1003-
function (err) {
1004-
expect(err).to.not.exist;
1005-
// Test return new document on change
1006-
collection.insert(
1007-
[{ name: 'test1' }, { name: 'test2' }],
1008-
configuration.writeConcernMax(),
1009-
function (err) {
1010-
expect(err).to.not.exist;
1011-
// Let's modify the document in place
1012-
collection.findOneAndUpdate(
1013-
{ name: 'test1' },
1014-
{ $set: { name: 'test2' } },
1015-
{},
1016-
function (err, updated_doc) {
1017-
expect(err).to.exist;
1018-
expect(updated_doc).to.not.exist;
1019-
client.close(done);
1020-
}
1021-
);
1022-
}
1023-
);
1024-
}
1025-
);
1026-
});
1027-
});
1028-
}
1029-
});
1030-
1031864
it('Should correctly return null when attempting to modify a non-existing document', {
1032865
metadata: {
1033866
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
@@ -1047,7 +880,7 @@ describe('Find', function () {
1047880
{ $set: { name: 'test2' } },
1048881
{},
1049882
function (err, updated_doc) {
1050-
expect(updated_doc.value).to.not.exist;
883+
expect(updated_doc).to.not.exist;
1051884
test.ok(err == null || err.errmsg.match('No matching object found'));
1052885
client.close(done);
1053886
}
@@ -1168,8 +1001,8 @@ describe('Find', function () {
11681001
{ $set: { b: 3 } },
11691002
{ returnDocument: ReturnDocument.AFTER },
11701003
function (err, result) {
1171-
test.equal(2, result.value.a);
1172-
test.equal(3, result.value.b);
1004+
test.equal(2, result.a);
1005+
test.equal(3, result.b);
11731006
p_client.close(done);
11741007
}
11751008
);
@@ -1260,12 +1093,12 @@ describe('Find', function () {
12601093
{ $set: { 'c.c': 100 } },
12611094
{ returnDocument: ReturnDocument.AFTER },
12621095
function (err, item) {
1263-
test.equal(doc._id.toString(), item.value._id.toString());
1264-
test.equal(doc.a, item.value.a);
1265-
test.equal(doc.b, item.value.b);
1266-
test.equal(doc.c.a, item.value.c.a);
1267-
test.equal(doc.c.b, item.value.c.b);
1268-
test.equal(100, item.value.c.c);
1096+
test.equal(doc._id.toString(), item._id.toString());
1097+
test.equal(doc.a, item.a);
1098+
test.equal(doc.b, item.b);
1099+
test.equal(doc.c.a, item.c.a);
1100+
test.equal(doc.c.b, item.c.b);
1101+
test.equal(100, item.c.c);
12691102
client.close(done);
12701103
}
12711104
);
@@ -1455,7 +1288,7 @@ describe('Find', function () {
14551288
{ returnDocument: ReturnDocument.AFTER },
14561289
function (err, updated_doc) {
14571290
expect(err).to.not.exist;
1458-
expect(updated_doc.value).to.not.exist;
1291+
expect(updated_doc).to.not.exist;
14591292
client.close(done);
14601293
}
14611294
);
@@ -2327,8 +2160,8 @@ describe('Find', function () {
23272160
{ $set: { b: 3 } },
23282161
{ returnDocument: ReturnDocument.AFTER },
23292162
function (err, updated_doc) {
2330-
test.equal(1, updated_doc.value.a);
2331-
test.equal(3, updated_doc.value.b);
2163+
test.equal(1, updated_doc.a);
2164+
test.equal(3, updated_doc.b);
23322165

23332166
client.close(done);
23342167
}

test/integration/crud/insert.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ describe('crud - insert', function () {
886886
serializeFunctions: true
887887
},
888888
function (err, result) {
889-
test.ok(result.value.f._bsontype === 'Code');
889+
test.ok(result.f._bsontype === 'Code');
890890
client.close(done);
891891
}
892892
);

0 commit comments

Comments
 (0)