ยง2023-08-08

you add shards first before you create your database in shard.

alexlai@N2Jammy:~$ mv ~/.mongoshrc.js ~/.mongoshrc.js.ori
alexlai@N2Jammy:~$ mongosh mongodb://orgpi5Arch.yushei.net:27981
Current Mongosh Log ID:	64d1ce67d275f01e8aedb443
Connecting to:		mongodb://orgpi5Arch.yushei.net:27981/?directConnection=true&appName=mongosh+1.10.1
Using MongoDB:		7.0.0-rc8
Using Mongosh:		1.10.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-08-08T11:02:18.683+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

[direct: mongos] test> use admin
switched to db admin
[direct: mongos] admin> sh.addShard("ShardA/n2Mnjaro.yushei.net:27985");
{
  shardAdded: 'ShardA',
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1691471885, i: 6 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1691471885, i: 6 })
}
[direct: mongos] admin> sh.addShard("ShardB/n2Mnjaro.yushei.net:27983");
{
  shardAdded: 'ShardB',
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1691471907, i: 3 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1691471906, i: 5 })
}
[direct: mongos] admin> sh.status()
shardingVersion
{ _id: 1, clusterId: ObjectId("64d17cee5659ccac70b105c0") }
---
shards
[
  {
    _id: 'ShardA',
    host: 'ShardA/hc4Bookworm.yushei.net:27985,hc4Lunar.yushei.net:27985,n2Mnjaro.yushei.net:27985',
    state: 1,
    topologyTime: Timestamp({ t: 1691471885, i: 3 })
  },
  {
    _id: 'ShardB',
    host: 'ShardB/hc4Bookworm.yushei.net:27983,hc4Lunar.yushei.net:27983,n2Mnjaro.yushei.net:27983',
    state: 1,
    topologyTime: Timestamp({ t: 1691471906, i: 3 })
  }
]
---
active mongoses
[ { '7.0.0-rc8': 1 } ]
---
autosplit
{ 'Currently enabled': 'yes' }
---
balancer
{ 'Currently enabled': 'yes', 'Currently running': 'no' }
---
databases
[
  {
    database: { _id: 'config', primary: 'config', partitioned: true },
    collections: {
      'config.system.sessions': {
        shardKey: { _id: 1 },
        unique: false,
        balancing: true,
        chunkMetadata: [ { shard: 'ShardA', nChunks: 1024 } ],
        chunks: [
          'too many chunks to print, use verbose if you want to force print'
        ],
        tags: []
      }
    }
  }
]
[direct: mongos] admin> sh.enableSharding("Learn")
{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1691473198, i: 8 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1691473198, i: 3 })
}