Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-1122

MongoDB Shell Incorrectly Parses sh.shardCollection Arguments

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • No version
    • Affects Version/s: 1.1.9
    • Component/s: Shell API
    • Labels:
    • 2
    • Needed
    • Hide

      For sh.shardCollection():

      • In the legacy shell, the options argument cannot be specified without also providing the unique argument, i.e. sh.shardCollection(coll, key, unique, options) is valid but sh.shardCollection(coll, key, options) is not. The docs mention this as “If specifying the options document, you must explicitly specify the value for unique”.
      • In mongosh, this restriction is gone, i.e. sh.shardCollection(coll, key, options) works, with unique defaulting to false unless a unique key is specified in the options argument.

      The docs should be updated to reflect this more flexible behavior by mongosh.

      Show
      For sh.shardCollection() : In the legacy shell, the options argument cannot be specified without also providing the unique argument, i.e. sh.shardCollection(coll, key, unique, options) is valid but sh.shardCollection(coll, key, options) is not . The docs mention this as “If specifying the options document, you must explicitly specify the value for unique”. In mongosh, this restriction is gone, i.e. sh.shardCollection(coll, key, options) works, with unique defaulting to false unless a unique key is specified in the options argument. The docs should be updated to reflect this more flexible behavior by mongosh.
    • Iteration Yonkers, Iteration Zanzibar, Iteration Cuttlefish, Iteration Dolphin

      Hi Team,

      I was testing the sh.shardCollection method in the New MongoDB Shell and realized that it appears to incorrectly parse the arguments when the "options" document is included. Here's an example:

      mlaunch init --sharded 2 --replicaset --csrs
      
      db.getSiblingDB("test").createCollection("mongosh", { collation: { locale: "en", strength: 2 } } )
      
      db.getSiblingDB("test").getCollection("mongosh").createIndex( { a: 1 }, { collation: { locale: "simple" } } )
      
      sh.enableSharding("test")
      
      sh.shardCollection("test.mongosh", { a: 1 }, { collation: { locale: "simple" } } )
      

      This results in the collection being sharded with the unique property set to "false":

            'test.mongosh': {
              shardKey: { a: 1 },
              unique: false,
              balancing: true,
      

      On the contrary, if you use the Legacy MongoDB Shell you receive the following error when executing the sh.shardCollection command in the same way:

      mlaunch init --sharded 2 --replicaset --csrs
      
      db.getSiblingDB("test").createCollection("mongo", { collation: { locale: "en", strength: 2 } } )
      
      db.getSiblingDB("test").getCollection("mongo").createIndex( { a: 1 }, { collation: { locale: "simple" } } )
      
      sh.enableSharding("test")
      
      sh.shardCollection("test.mongo", { a: 1 }, { collation: { locale: "simple" } } )
      {
      	"ok" : 0,
      	"errmsg" : "Collection has default collation: collation: { locale: \"en\", caseLevel: false, caseFirst: \"off\", strength: 2, numericOrdering: false, alternate: \"non-ignorable\", maxVariable: \"punct\", normalization: false, backwards: false, version: \"57.1\" }. Must specify collation {locale: 'simple'}",
      	"code" : 2,
      	"codeName" : "BadValue",
      	"operationTime" : Timestamp(1644510736, 4),
      	"$clusterTime" : {
      		"clusterTime" : Timestamp(1644510736, 4),
      		"signature" : {
      			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
      			"keyId" : NumberLong(0)
      		}
      	}
      }
      

      Based on sh.shardCollection, the Legacy MongoDB Shell behavior is the right one and the New MongoDB Shell should be fixed:

      If specifying the options document, you must explicitly specify the value for unique.

      Would you please review this?

      Regards
      Diego

            Assignee:
            anna.henningsen@mongodb.com Anna Henningsen
            Reporter:
            diego.rodriguez@mongodb.com Diego Rodriguez (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: