[GODRIVER-743] UpdateOne and UpdateMany should return error when update parameter is of zero length Created: 03/Jan/19  Updated: 23/Jan/19  Resolved: 23/Jan/19

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: 0.1.0
Fix Version/s: 0.3.0

Type: Bug Priority: Minor - P4
Reporter: ollevche Assignee: Divjot Arora (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 18.04


Attachments: PNG File after.png     PNG File before.png    

 Description   

The UpdateOne and UpdateMany methods do not ensure that the update parameter has at least one modifier. Update the ensureDollarKey method to return an error if there are no modifiers provided.

(original description below)


mongo.UpdateOne() erases all fields from a document matched by a filter when update interface is bson.D{} with zero length. That behavior is unwanted in my opinion (i've lost some data). Mongo shell acts differently (just returns an error 'the update operation document must contain at least one atomic operator'). I didn't check Update() and UpdateMany() but these ones may act the same as UpdateOne().



 Comments   
Comment by Githook User [ 23/Jan/19 ]

Author:

{'email': 'divjot.arora@10gen.com', 'name': 'Divjot Arora', 'username': 'divjotarora'}

Message: Disallow empty update documents.

GODRIVER-743

Change-Id: I57ef34e7ce9368128c560fef056eff1ccede9e3b
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/ecb738ac507d856715f13423d914d8fd99ac880c

Comment by Kristofer Brandow (Inactive) [ 08/Jan/19 ]

jeff.yemin the driver does support that, but there's a bug when there are no elements provided. I'll update this ticket and schedule it.

Comment by Jeffrey Yemin [ 08/Jan/19 ]

Looks like the driver does not conform to this section of the CRUD specification: https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#update-vs-replace-validation.

The update family of operations require that the update document parameter MUST have only atomic modifiers. In practice, this means that introspection needs to happen on that document to enforce this. However, it is enough to only check the first element in the document. If it begins with a $ sign and the rest of the document's elements do not, the server will throw an error. Note that it is required that an update document have at least one atomic modifier.

The driver should be reporting this as an error and not sending the operation to the server.

Comment by ollevche [ 08/Jan/19 ]

Yes, sure. In addition to the code, i added screenshots of the matched document before and after a function call.

Something like that will erase all fields (except '_id' field) from a matched document:

 
func UpdateTest(db *mongo.Database) {

    id, _ := primitive.ObjectIDFromHex("5c346258740d8e1c1c504455")
    filter := bson.D{{"_id", id}}

    db.Collection("students").UpdateOne(nil, filter, bson.D{})
}
 
Before:

After:

Comment by Kristofer Brandow (Inactive) [ 07/Jan/19 ]

Hi ollevche,

Can you please provide a working code example of this?

Thanks,

Kris

Comment by ollevche [ 07/Jan/19 ]

@kris.brandow it's a typo, sorry. I'm using the latest one (0.1.0).

Comment by Kristofer Brandow (Inactive) [ 04/Jan/19 ]

Hi ollevche,

Can you upgrade to the latest beta (0.1.0) and try again to see if the problem persists?

Thanks,

Kris

Comment by ollevche [ 03/Jan/19 ]

I'm using 0.0.10 version (not a 0.0.15).

Generated at Thu Feb 08 08:34:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.