[SERVER-1639] Failed to update an object with null value in shard key Created: 19/Aug/10  Updated: 29/May/12  Resolved: 26/Aug/10

Status: Closed
Project: Core Server
Component/s: Sharding
Affects Version/s: 1.6.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Che-Ching Wu Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-1689 Only allow equality matches on shard ... Closed
Operating System: ALL
Participants:

 Description   

// There are 2 documents in collection virus already.
> db.virus.find()

{ "_id" : ObjectId("4c6cf85f1dc3e52659865ff2"), "did" : 1, "recv_time" : 1, "n" : 1 } { "_id" : ObjectId("4c6cf86f1dc3e52659865ff3"), "did" : 1, "recv_time" : 2, "n" : 2 }

// Using shard key and operator in criteria, and using modifier $set in objNew
> db.virus.update({did:1,recv_time:{$gte:3}},{$set:{n:3}},true,false)

// document inserted even recv_time is null
> db.virus.find()

{ "_id" : ObjectId("4c6cf85f1dc3e52659865ff2"), "did" : 1, "recv_time" : 1, "n" : 1 } { "_id" : ObjectId("4c6cf86f1dc3e52659865ff3"), "did" : 1, "recv_time" : 2, "n" : 2 } { "_id" : ObjectId("4c6cf8f55d602a71c933a105"), "did" : 1, "n" : 3 }

// could query with recv_time==null
> db.virus.find(

{recv_time:null}

)

{ "_id" : ObjectId("4c6cf8f55d602a71c933a105"), "did" : 1, "n" : 3 }

// Can't update via ObjectId
> db.virus.update({_id:ObjectId("4c6cf8f55d602a71c933a105")},

{n:4}

)
right object doesn't have full shard key

// Can't update by criteria with shard key
> db.virus.update(

{did:1,recv_time:null}

,

{n:4}

)
shard key must be in update object



 Comments   
Comment by Mathias Stearn [ 26/Aug/10 ]

Ahh, I see the issue. Sharded updates must know exactly the shard_key of the document you wish to update. The code currently only checks that you have something for the shard key, not that it is an exact match. I've created a case for that and linked it to this one.

For your current situation I'd suggest deleting all objects w/o a shard key, and modifying your code to ensure that all updates (especially upserts) include an exact match for the shard key.

Comment by Che-Ching Wu [ 26/Aug/10 ]

Here is the way:

> db.virus.update({did:1,recv_time:{$gte:3}},{$set:{n:3}},true,false)

Comment by Mathias Stearn [ 25/Aug/10 ]

I take it back, I don't think there is a bug in update, I just typoed while testing:

> db.bar.find()

{ "_id" : ObjectId("4c755696d41c2ec36db0626b"), "key" : 1 } { "_id" : ObjectId("4c755699d41c2ec36db0626c"), "key" : null }

> db.bar.update(

{ "_id" : ObjectId("4c755699d41c2ec36db0626c"), "key" : null }

, {$set: {n:1}})
> db.bar.find()

{ "_id" : ObjectId("4c755696d41c2ec36db0626b"), "key" : 1 } { "_id" : ObjectId("4c755699d41c2ec36db0626c"), "key" : null, "n" : 1 }

> db.bar.update(

{ "_id" : ObjectId("4c755699d41c2ec36db0626c")}

,

{"key" : null, n:2}

)
> db.bar.find()

{ "_id" : ObjectId("4c755696d41c2ec36db0626b"), "key" : 1 } { "_id" : ObjectId("4c755699d41c2ec36db0626c"), "key" : null, "n" : 2 }

I'm still curious how you managed to insert an object w/o the shard key. Mongos should be preventing that and if it isn't then that is the bug

Comment by Mathias Stearn [ 25/Aug/10 ]

How did you insert an object w/o a shard key? You should get an error like this:

> db.bar.insert(

{key:1}

)
> db.bar.insert(

{key:null}

)
> db.bar.insert({})
tried to insert object without shard key
> db.bar.find()

{ "_id" : ObjectId("4c755696d41c2ec36db0626b"), "key" : 1 } { "_id" : ObjectId("4c755699d41c2ec36db0626c"), "key" : null }

Note that explicitly storing null is different from missing fields, even though querying with null will match missing fields.

As for the updates, there is an actual bug, but your update commands are also incorrect. I beleave you want one of these (for an explicit null):

db.virus.update(

{did:1,recv_time:null}

,{$set:{n:4}})
db.virus.update({_id: some_id},

{did:1, recv_time:null, n:4}

)

see http://www.mongodb.org/display/DOCS/Updating#Updating-update%28%29 for the difference between the two

Generated at Thu Feb 08 02:57:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.