[SERVER-61683] Operation findAndModify not possible on hashed sharded collection Created: 22/Nov/21  Updated: 27/Oct/23  Resolved: 25/Nov/21

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

Type: Bug Priority: Major - P3
Reporter: Wernfried Domscheit Assignee: Backlog - Triage Team
Resolution: Community Answered Votes: 0
Labels: findAndModify, hashed, sharding
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-44422 Allow findAndModify and delete one to... Closed
Assigned Teams:
Server Triage
Operating System: ALL
Steps To Reproduce:

 

db.createCollection('dummy')
db.dummy.createIndex({ ts: "hashed" })
sh.shardCollection(db.getName() + '.dummy', { ts: "hashed" })
db.dummy.insertOne({ _id: 1, ts: ISODate(), a: [] })
 
db.dummy.findOneAndUpdate(
   { _id: 1 },
   { $push: { action: { a: 1 } } },
   { returnNewDocument: true }
)
 
uncaught exception: Error: findAndModifyFailed failed: {
	"ok" : 0,
	"errmsg" : "Query for sharded findAndModify must contain the shard key",
	"code" : 61,
	"codeName" : "ShardKeyNotFound",
	"$clusterTime" : {
		"clusterTime" : Timestamp(1637567453, 1),
		"signature" : {
			"hash" : BinData(0,"LyZC2RQslxgP9O7xKZ6NjXFuioo="),
			"keyId" : NumberLong("7025509753912557590")
		}
	},
	"operationTime" : Timestamp(1637567453, 1)
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:742:15
DBCollection.prototype.findOneAndUpdate@src/mongo/shell/crud_api.js:910:12
@(shell):1:1

 

 

I tried workarounds like

 

db.dummy.findOneAndUpdate(
   { _id: 1, ts: { $lte: ISODate() } },
   { $push: { action: { a: 1 } } },
   { returnNewDocument: true }
)
 
db.dummy.findOneAndUpdate(
   { _id: 1, ts: { $exists: true } },
   { $push: { action: { a: 1 } } },
   { returnNewDocument: true }
)

 

 

But none of them is working. I don't find any possibility to query for a hashed value. Do you have any solution?

Kind Regards
Wernfried

 

 

Participants:

 Description   

When I have a sharded collection with a hashed shard key then findAndModify does not work. I constantly get error "Query for sharded findAndModify must contain the shard key" and I find no way to get rid of it.

 

 



 Comments   
Comment by Dmitry Agranat [ 25/Nov/21 ]

Hi wernfried.domscheit@sunrise.net,

The SERVER project is for bugs and feature suggestions for the MongoDB server. For these questions, we'd like to encourage you to start by asking our community for help by posting on the MongoDB Developer Community Forums.

If the discussion there leads you to suspect a bug in the MongoDB server, then we'd want to investigate it as a possible bug here in the SERVER project.

Regards,
Dima

Comment by Wernfried Domscheit [ 22/Nov/21 ]

OK,  this one is working:

 

db.dummy.insertOne({ _id: 1, ts: ISODate("2021-11-22T17:00:00Z"), a: [] })
 
db.dummy.findOneAndUpdate(
   { _id: 1, ts: ISODate("2021-11-22T17:00:00Z") },
   { $push: { action: { a: 1 } } },
   { returnNewDocument: true }
)

But typically I don't know the timestamp, because it is the current time ISODate(), I only know the _id which is the primary key.

Is there any solution for this?

Wernfried

 

 

Comment by Dmitry Agranat [ 22/Nov/21 ]

wernfried.domscheit@sunrise.net, based on our documentation:

the query filter must include an equality condition on the shard key.

In other words, you must have equality condition/match on your hashed shard key named ts

Comment by Wernfried Domscheit [ 22/Nov/21 ]

The shard key is hash value, how can I query it?

The basic idea of a hash value is, that I don't know in which shard the data is, I only know the documents should be evenly distributed.

Wernfried

 

 

 

Comment by Dmitry Agranat [ 22/Nov/21 ]

Hi wernfried.domscheit@sunrise.net,

Could you query the field that contains an exact match on the shard key, for example with $eq? If this does not work, could you also post the output from sh.status()?

Dima

Generated at Thu Feb 08 05:53:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.