[DOCS-3039] Document the limitation of removing a single document in sharded the collection Created: 31/Mar/14  Updated: 01/Apr/14  Resolved: 01/Apr/14

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: v1.3.3

Type: Bug Priority: Major - P3
Reporter: Gianfranco Palumbo Assignee: Sam Kleinman (Inactive)
Resolution: Done Votes: 0
Labels: sprint-slipstream
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File test_this_thing.js    
Issue Links:
Related
Participants:
Days since reply: 9 years, 46 weeks, 1 day ago

 Description   

If you delete only one document (using justOne option) without querying by _id, like so:

db.collection.remove({a:1}, true)

you'll get this assertion:

can only delete with a non-shard key pattern if can delete as many as we find

This is a restriction for sharded collections. See source code:
https://github.com/mongodb/mongo/blob/v2.4/src/mongo/s/strategy_shard.cpp#L1153

A warning/note should be added here:
http://docs.mongodb.org/manual/reference/method/db.collection.remove/

This limitation still exists in 2.6



 Comments   
Comment by Githook User [ 01/Apr/14 ]

Author:

{u'username': u'tychoish', u'name': u'Sam Kleinman', u'email': u'samk@10gen.com'}

Message: DOCS-3039: being more clear about operations affecting single documents
Branch: master
https://github.com/mongodb/docs/commit/b4dc16066a1aa9d322b3d6a17df6adbfd67433a9

Comment by Githook User [ 01/Apr/14 ]

Author:

{u'username': u'tychoish', u'name': u'Sam Kleinman', u'email': u'samk@10gen.com'}

Message: DOCS-3039: added single doc modification note to remove/update pages
Branch: master
https://github.com/mongodb/docs/commit/bedea4fd725384431929252a228b5f2dabcba3e9

Comment by Gianfranco Palumbo [ 01/Apr/14 ]

My bad. I must have tested this in a not sharded database+collection.

mongos> sh.status()
--- Sharding Status ---
  sharding version: {
	"_id" : 1,
	"version" : 4,
	"minCompatibleVersion" : 4,
	"currentVersion" : 5,
	"clusterId" : ObjectId("5339a539a5ac65253059d2b2")
}
  shards:
	{  "_id" : "shard0000",  "host" : "local:37018" }
	{  "_id" : "shard0001",  "host" : "local:37019" }
  databases:
	{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
	{  "_id" : "test",  "partitioned" : true,  "primary" : "shard0000" }
		test.test
			shard key: { "a" : 1 }
			chunks:
				shard0000	1
			{ "a" : { "$minKey" : 1 } } -->> { "a" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)
 
mongos> db.test.remove({b: 1}, true)
WriteResult({
	"nRemoved" : 0,
	"writeError" : {
		"code" : 61,
		"errmsg" : "delete { q: { b: 1.0 }, limit: 1 } does not contain _id or shard key for pattern { a: 1.0 }"
	}
})

Comment by Scott Hernandez (Inactive) [ 01/Apr/14 ]

This is not correct and should not change for 2.6. The is a design of sharding and a single (or limited) delete/update is not supported without the shard key, as the error message says.

Here is the error message from 2.6:

{	"err" : "delete { q: {}, limit: 1 } does not contain _id or shard key for pattern { a: 1.0 }",
	"code" : 61,
	"n" : 0,
	"shards" : [ ],
	"shardRawGLE" : {},
	"ok" : 1 }
// or from a write command:
{	"nRemoved" : 0,
	"writeError" : {
		"code" : 61,
		"errmsg" : "delete { q: {}, limit: 1 } does not contain _id or shard key for pattern { a: 1.0 }" }}

Comment by Gianfranco Palumbo [ 31/Mar/14 ]

Tested. This limitation does not exists 2.6:

For example on 2.4:
Shard key

{a: 1}

mongos> db.test.insert({a: 1, b: 1})
mongos> db.test.insert({a: 2, b: 1})
mongos> db.test.insert({a: 3, b: 1})
mongos> db.test.insert({a: 1, b: 2})
mongos> db.test.count()
4
mongos> db.test.remove({b: 1}, true)
can only delete with a non-shard key pattern if can delete as many as we find : { b: 1.0 }
mongos> db.test.remove({a: 1}, true)
mongos> db.test.count()
3

On 2.6-rc2:

mongos> use test
switched to db test
mongos> db.adminCommand( { enableSharding : "test" } )
{ "ok" : 1 }
mongos> db.adminCommand( { shardCollection : "test.test", key : {"a": 1} } )
{ "collectionsharded" : "test.test", "ok" : 1 }
mongos> db.test.find()
{ "_id" : ObjectId("5339a679a4c59031f63a2359"), "a" : 1, "b" : 1 }
{ "_id" : ObjectId("5339a67ba4c59031f63a235a"), "a" : 2, "b" : 1 }
{ "_id" : ObjectId("5339a67ea4c59031f63a235b"), "a" : 3, "b" : 1 }
{ "_id" : ObjectId("5339a690a4c59031f63a235c"), "a" : 1, "b" : 2 }
mongos> db.test.remove({b: 1}, true)
WriteResult({ "nRemoved" : 1 })
mongos> db.test.remove({a: 1}, true)
WriteResult({ "nRemoved" : 1 })
mongos> db.test.count()
2

Comment by Gianfranco Palumbo [ 31/Mar/14 ]

I found that we actually have this in our limits reference page.
http://docs.mongodb.org/manual/reference/limits/#Single-Document-Modification-Operations-in-Sharded-Collections

We should put this in the update() and remove() methods pages.
It's more likely that you'll look into how to do a single document modification or removal from those pages rather than from the limits page.

Generated at Thu Feb 08 07:44:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.