[CSHARP-1836] Legacy v2.x FindAndModify operation returns modified document by default (instead of original) Created: 23/Nov/16  Updated: 19/Dec/16  Resolved: 07/Dec/16

Status: Closed
Project: C# Driver
Component/s: Operations
Affects Version/s: 2.0
Fix Version/s: 2.4.1

Type: Bug Priority: Major - P3
Reporter: Maksim Krautsou [X] Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

According to documentation, FindAndModify method should return original document by default.

But, it seems like, without specifying VersionReturned it returns modified document.
I looked at the MongoCollection class (in github) and at the line #560 it does a check. And, because the default value of VersionReturned property is null, then condition returns false. Therefore, Core.Operations.ReturnDocument.After will be used.

It case of using the following overloading of method:

public virtual FindAndModifyResult FindAndModify(IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update)
{
    var args = new FindAndModifyArgs { Query = query, SortBy = sortBy, Update = update };
    return FindAndModify(args);
}

It doesn't specify which version of document should be returned.

According to this fragment:

var returnDocument = args.VersionReturned == FindAndModifyDocumentVersion.Original
    ? Core.Operations.ReturnDocument.Before
    : Core.Operations.ReturnDocument.After;

It will be the same as:

var returnDocument = (FindAndModifyDocumentVersion?)null == FindAndModifyDocumentVersion.Original
    ? Core.Operations.ReturnDocument.Before
    : Core.Operations.ReturnDocument.After;

Need to add checking for null value and handle null as a default (original\before document) value.



 Comments   
Comment by Githook User [ 07/Dec/16 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1836: Added Maksim Krautsou to the contributors list in README.md.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/a417730fc4225cb934728c2abeb056ad53901952

Comment by Githook User [ 07/Dec/16 ]

Author:

{u'username': u'MaKCbIMKo', u'name': u'Maksim Krautsou', u'email': u'Maksim_Krautsou@epam.com'}

Message: CSHARP-1836: Added additional check for default 'VersionReturned' property value and added unit tests.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/d8ca73e21df8da7eed239cc89d5ee169ecd14d13

Comment by Robert Stam [ 06/Dec/16 ]

This bug only exists in the Legacy API of the v2.x versions of the driver.

The v1.x versions and the new CRUD API in the v2.x versions correctly default to return the original document.

Comment by Robert Stam [ 30/Nov/16 ]

This is indeed a bug.

Will have to think about whether fixing it is a backward breaking change.

Generated at Wed Feb 07 21:40:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.