文档首页 > > 用户指南> 数据库命令> 命令回显的差异(社区增强版)

命令回显的差异(社区增强版)

分享
更新时间: 2020/03/12 GMT+08:00

为了有效地缩减数据迁移的时间,DDS社区增强版的命令在社区版的基础上进行了优化,优化后命令的语义以及传参不变,部分命令的回显内容与社区版存在差异,具体请参见表1

表1 DDS社区增强版和社区版命令回显差异

命令

含义

社区增强版

社区版

db.collection.stats()

显示表的统计信息。

回显中不包含shard节点的统计信息。

mongos > sh.shardCollection("test.tab",{a:1})
{ "collectionsharded" : "test.tab", "ok" : 1 }
mongos > db.tab.stats()
{
        "sharded" : true,
        "capped" : false,
        "ns" : "test.tab",
        "count" : 0,
        "size" : 0,
        "storageSize" : 256,
        "totalIndexSize" : 2,
        "indexSizes" : {
                "_id_" : 1,
                "a_1" : 1
        },
        "avgObjSize" : 0,
        "nindexes" : 2,
        "nchunks" : 1,
        "ok" : 1
}

回显中包含shard节点的统计信息。

mongos > sh.shardCollection("test.tab",{a:1})
{ "collectionsharded" : "test.tab", "ok" : 1 }
mongos> db.tab.stats()
{
        "sharded" : true,
        "capped" : false,
        "ns" : "test.tab",
        "count" : 0,
        "size" : 0,
        "storageSize" : 256,
        "totalIndexSize" : 2,
        "indexSizes" : {
                "_id_" : 1,
                "b_1" : 1
        },
        "avgObjSize" : 0,
        "nindexes" : 2,
        "nchunks" : 1,
        "shards" : {
                "shard0000" : {
                        "ns" : "test.tab",
                        "size" : 0,
                        "count" : 0,
                        "storageSize" : 256,
                        "capped" : false,
                        "nindexes" : 2,
                        "totalIndexSize" : 3,
                        "indexSizes" : {
                                "_id_" : 1,
                                "b_1" : 1
                        },
                        "ok" : 1
                }
        },
        "ok" : 1
}

db.currentOp()

查看当前数据库上正在执行的操作。

回显信息中创建表的操作名称为“assignChunk”,删除表的操作名称为“offloadChunk”。此外,出于安全考虑,shard的IP和Port由shard的ID名称代替。

mongos> db.adminCommand({currentOp:1})
{
"raw" : {
    "shard0000" : {
           "inprog" : [
                   {
                     "desc" : "conn6",
                     "threadId" : "140699745277696",
                     "connectionId" : 6,
                     "client" : "10.0.0.1:2201",
                     "appName" : "MongoDB Shell",
                     "active" : true,
                     "opid" : 1977,
                     "secs_running" : 0,
                     "microsecs_running" : NumberLong(61),
                     "op" : "command",
                     "ns" : "admin.",
                     "query" : {
                           "offloadChunk" : "test.tab",
                           "chunk" : {
                                "_id" : "00000000000001",
                                "ns" : "test.tab",
                                "min" : {
                                      "_id" : { "$minkey" : 1 }
                                 },
                                "max" : {
                                      "_id" : { "$maxkey" : 1 }
                                 },
                                "shard" : "shard0000",
                                "processIdentity" : "94860_15203344522245",
                                "lastmod" : Timestamp(1,1048675),
                                "rootFolder" : "/data/db/job0/resmoke/data/1",
                                "status" : 1
                                }
                             },
                             "numYields" : 0,
                             "locks" : {
 
                              },
                             "waitingForLock" : false,
                             "lockStats" : {
 
                             }
                           }
                        ],
                        "ok" : 1
                }
        "inprog" : [
                {
                        "desc" : "conn6",
                        "threadId" : "140699745277696",
                        "connectionId" : 6,
                        "client_s" : "10.0.0.1:2201",
                        "appName" : "MongoDB Shell",
                        "active" : true,
                        "opid" : "shard0000:1977",
                        "secs_running" : 0,
                        "microsecs_running" : NumberLong(61),
                        "op" : "command",
                        "ns" : "admin.",
                        "query" : {
                             "offloadChunk" : "test.tab",
                             "chunk" : {
                                    "_id" : "00000000000001",
                                    "ns" : "test.tab",
                                    "min" : {
                                           "_id" : { "$minkey" : 1 }
                                     },
                                    "max" : {
                                           "_id" : { "$maxkey" : 1 }
                                     },
                                    "shard" : "shard0000",
                                    "processIdentity" : "94860_15203344522245",
                                    "lastmod" : Timestamp(1,1048675),
                                    "rootFolder" : "/data/db/job0/resmoke/data/1",
                                    "status" : 1
                             }                                  
                        },
                        "numYields" : 0,
                        "locks" : {
 
                        },
                        "waitingForLock" : false,
                        "lockStats" : {
 
                        }
                }
            ],
            "ok" : 1
}

回显信息中创建表的操作名称为”shardCollection”和“create”,删除表的操作名称为“drop”。

mongos> db.adminCommand({currentOp:1})
{
"raw" : {
    "10.0.0.1:2200" : {
           "inprog" : [
                   {
                     "desc" : "conn6",
                     "threadId" : "140699745277696",
                     "connectionId" : 6,
                     "client" : "10.0.0.1:2201",
                     "appName" : "MongoDB Shell",
                     "active" : true,
                     "opid" : 1977,
                     "secs_running" : 0,
                     "microsecs_running" : NumberLong(61),
                     "op" : "command",
                     "ns" : "admin.",
                     "query" : {
                           "drop" : "test.tab"
                      },
                     "numYields" : 0,
                     "locks" : {
 
                     },
                     "waitingForLock" : false,
                     "lockStats" : {
 
                             }
                           }
                        ],
                        "ok" : 1
                }
        "inprog" : [
                {
                        "desc" : "conn6",
                        "threadId" : "140699745277698",
                        "connectionId" : 9,
                        "client_s" : "10.0.0.1:2201",
                        "appName" : "MongoDB Shell",
                        "active" : true,
                        "opid" : "shard0000:1977",
                        "secs_running" : 0,
                        "microsecs_running" : NumberLong(61),
                        "op" : "command",
                        "ns" : "admin.",
                        "query" : {
                            "drop" : "test.tab"
                        },
                        "numYields" : 0,
                        "locks" : {
 
                        },
                        "waitingForLock" : false,
                        "lockStats" : {
 
                        }
                }
            ],
            "ok" : 1
}

db.collection.createIndex()

创建索引。

  • 当前版本下创建索引的回显,屏蔽掉shard节点IP和Port:
    mongos> db.createCollection("tab")
    { "ok" : 1 }
    mongos> db.tab.createIndex({b:1})
    {
        "raw" : {
    "shard0000" : {
            "numIndexes" : 2,
             "ok" : 1
      }
         },
        "ok" : 1
    }
  • 兼容的低版本下创建索引的回显:
    mongos> coll = db.tab
    test.tab
    mongos> 
    mongos> request = {
                 insert: "system.indexes",
                 documents: [{ns: coll.toString(), key: {x: 1}, name: "x_1"}]
    };
    {
            "insert" : "system.indexes",
            "documents" : [
                    {
                            "ns" : "test.tab",
                            "key" : {
                                    "x" : 1
                            },
                            "name" : "x_1"
                    }
            ]
    }
    mongos> result = coll.runCommand(request);
    { "ok" : 1, "n" : 1 }
    mongos> result = coll.runCommand(request);
    { "ok" : 1, "n" : 1 }
  • 当前版本下创建索引的回显:
    mongos> db.createCollection("tab")
    { "ok" : 1 }
    mongos> db.tab.createIndex({b:1})
    {
            "raw" : {
                    "10.183.157.9:2200" : {
                            "createdCollectionAutomatically" : false,
                            "numIndexesBefore" : 1,
                            "numIndexesAfter" : 2,
                            "ok" : 1
                    }
            },
            "ok" : 1
    }
  • 兼容的低版本下创建索引的回显:
    mongos> coll = db.tab
    test.tab
    mongos> 
    mongos> request = {
                 insert: "system.indexes",
                 documents: [{ns: coll.toString(), key: {x: 1}, name: "x_1"}]
    };
    {
            "insert" : "system.indexes",
            "documents" : [
                    {
                            "ns" : "test.tab",
                            "key" : {
                                    "x" : 1
                            },
                            "name" : "x_1"
                    }
            ]
    }
    mongos> result = coll.runCommand(request);
    { "ok" : 1, "n" : 1 }
    mongos> result = coll.runCommand(request);
    { "ok" : 1, "n" : 0 }

sh.shardCollection()

对表中的数据进行分片。

重复执行该命令,回显中不包含非法操作错误码20。
mongos> sh.shardCollection("test.tab",{x:1})
{ "collectionsharded" : "test.tab", "ok" : 1 }
mongos> sh.shardCollection("test.tab",{x:1})
{ "collectionsharded" : "test.tab", "ok" : 1 }

重复执行该命令,回显中包含非法操作错误码20。

mongos> sh.shardCollection("test.tab",{x:1})
{ "collectionsharded" : "test.tab", "ok" : 1 }
mongos> sh.shardCollection("test.tab",{x:1})
{
        "code" : 20,
        "ok" : 0,
        "errmsg" : "sharding already enabled for collection test.tab"
}

db.collection.dropIndexes()

删除索引。

该命令对外屏蔽了“IndexNotFound”错误码。重复执行该命令,依旧执行成功。

mongos> db.tab.dropIndex({x:1})
{ "ok" : 1 }
mongos> db.tab.dropIndex({x:1})
{ "ok" : 1 }

重复执行该命令后将返回错误码信息。

mongos> db.tab.dropIndex({x:1})
{
        "raw" : {
                "10.0.0.1:2200" : {
                        "nIndexesWas" : 2,
                        "ok" : 1
                },
                "10.0.0.1:2201" : {
                        "ok" : 0,
                        "errmsg" : "ns not found",
                        "code" : 26,
                        "codeName" : "NamespaceNotFound"
                },
                "10.0.0.1:2202" : {
                        "ok" : 0,
                        "errmsg" : "ns not found",
                        "code" : 26,
                        "codeName" : "NamespaceNotFound"
                }
        },
        "code" : 26,
        "ok" : 0,
        "errmsg" : "{ 10.0.0.1:2200: \"ns not found\", 10.0.0.1:2201: \"ns not found\" }"
}
mongos> db.tab.dropIndex({x:1})
{
        "raw" : {
                "10.0.0.1:2200" : {
                        "nIndexesWas" : 1,
                        "ok" : 0,
                        "errmsg" : "can't find index with key: { x: 1.0 }",
                        "code" : 27,
                        "codeName" : "IndexNotFound"
                },
                "10.0.0.1:2201" : {
                        "ok" : 0,
                        "errmsg" : "ns not found",
                        "code" : 26,
                        "codeName" : "NamespaceNotFound"
                },
                "10.0.0.1:2202" : {
                        "ok" : 0,
                        "errmsg" : "ns not found",
                        "code" : 26,
                        "codeName" : "NamespaceNotFound"
                }
        },
        "ok" : 0,
        "errmsg" : "{ 10.0.0.1:2200: \"can't find index with key: { x: 1.0 }\", 10.0.0.1:2201: \"ns not found\", 10.0.0.1:2202: \"ns not found\" }"
}

sh.enableSharding()

使数据库能够支持创建分片。

对于用户创建的数据库,默认支持创建分片,可以直接执行sh.shardCollection()命令。

如果需要执行sh.shardCollection()命令对表进行分片,必须先执行sh.enableSharding()命令。

db.collection.reIndex()

重建索引。

重建索引命令的回显屏蔽shard节点IP和Port,此外不显示所有索引的详细信息。

mongos> db.tab.reIndex()
{
    "raw" : {
        "shard0002" : {
            "numIndexes" : 3,
            "ok" : 1
         },
        "shard0001" : {
             "numIndexes" : 3,
             "ok" : 1
         },
        "shard0000" : {
             "numIndexes" : 3,
              "ok" : 1
         }
    },
    "ok" : 1
}

创建索引命令的回显显示shard节点的IP和Port。命令的回显部分内容如下。

mongos> db.tab.reIndex()
{
   "raw" : {
      "10.183.157.9:2200" : {
          "nIndexesWas" : 3,
          "nIndexes" : 3,
           "indexes" : [
               {
                   "v" : 2,
                   "key" : {
                        "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "test.tab"
               },
               {
                     "v" : 2,
                     "key" : {
                         "a" : "hashed"
                      },
                      "name" : "a_hashed",
                      "ns" : "test.tab"
                },
                {
                       "v" : 2,
                       "key" : {
                           "b" : 1
                        },
                        "name" : "b_1",
                        "ns" : "test.tab"
                 }
             ],
            "ok" : 1
      }

db.collMod()

修改表或者视图的定义。

修改索引的过期时间,屏蔽了IP和Port。

mongos> db.runCommand( { collMod: "eventlog",index: { keyPattern: { lastModifiedDate: 1 },expireAfterSeconds: 36000}})
{
    "raw" : {
       "shard0001" : {
            "expireAfterSeconds_old" : 3600,
            "expireAfterSeconds_new" : 36000,
            "ok" : 1
        }
    },
    "ok" : 1
}

修改索引的过期时间。

mongos> db.runCommand( { collMod: "eventlog",index: { keyPattern: { lastModifiedDate: 1 },expireAfterSeconds: 36000}})
{
   "raw" : {
       "10.183.157.9:2200" : {
             "expireAfterSeconds_old" : 3600,
             "expireAfterSeconds_new" : 36000,
             "ok" : 1
        }
    },
    "ok" : 1
}

db.collection.explain()

返回查询计划的信息和查询计划的执行统计信息。

命令回显屏蔽连接信息和serverInfo。

mongos> db.mylog.explain().find()
{
    "queryPlanner" : {
        "mongosPlannerVersion" : 1,
        "winningPlan" : {
            "stage" : "SINGLE_SHARD",
            "shards" : [{
                shardName" : "shard0000",
                "connectionString" : "shard0000",
                "plannerVersion" : 1,
                "namespace" : "test.mylog",
                "indexFilterSet" : false,
                "parsedQuery" : { },
                "winningPlan" : {
                    "stage" : "COLLSCAN",
                    "direction" : "forward"
                },
                "rejectedPlans" : [ ]
            }]
        }
    },
    "ok" : 1
}

回显带有连接信息和serverInfo。

mongos> db.mylog.explain().find()
{
    "queryPlanner" : {
         "mongosPlannerVersion" : 1,
         "winningPlan" : {
               "stage" : "SINGLE_SHARD",
               "shards" : [{
               "shardName" : "shard0000",
               "connectionString" : "10.183.157.9:2200",
               "serverInfo" : {
                     "host" : "CTU1000090216",
                     "port" : 22001,
                     "version" : "3.4.0",
                     "gitVersion" : "b2ea16a16e00e365e5ab289284d6484043f6cd06"
         },
        "plannerVersion" : 1,
        "namespace" : "test.mylog",
        "indexFilterSet" : false,
        "parsedQuery" : {},
        "winningPlan" : {
            "stage" : "COLLSCAN",
            "direction" : "forward"
         },
         "rejectedPlans" : [ ]
         }]
       }
    },
    "ok" : 1
}

db.collection.aggregate()

聚合数据。

命令格式:

db.runCommand({
aggregate: "<collection>",
pipeline: [ <stage>, <...> ],
explain: <boolean>,
allowDiskUse: <boolean>,
cursor: <document>,
maxTimeMS: <int>,
bypassDocumentValidation: <boolean>,
readConcern: <document>,
collation: <document>
})

“pipeline”字段不支持操作符“$out”。

命令格式:

{
aggregate: "<collection>",
pipeline: [ <stage>, <...> ],
explain: <boolean>,
allowDiskUse: <boolean>,
cursor: <document>,
maxTimeMS: <int>,
bypassDocumentValidation: <boolean>,
readConcern: <document>,
collation: <document>
}

db.collection.renameCollection()

重命名表。

暂不支持系统表(例如:testdb.system.users)和普通用户表(例如:testdb.table)之间的重命名操作。

  • 普通用户表(例如:testdb.table)可以重命名为系统表(例如:testdb.system.users)。
  • 系统表可以重命名为普通用户表。

db.serverStatus().connections

查看当前数据库连接状态。

命令回显示例:

{ "current" : 1, "available" : 90, "totalCreated" : 2 }

“current”和“available”字段的值之和大于等于配置的参数“maxIncomingConnections”的值。

命令回显示例:

{ "current" : 1, "available" : 90, "totalCreated" : 2 }

“current”和“available”字段的值之和等于配置的参数“maxIncomingConnections”的值。

分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

跳转到云社区