db.stats()/ collections number&之间的差异db.getCollectionNames()计数

时间:2013-02-01 17:10:36

标签: mongodb mongodb-shell

我正在mongodb-shell尝试了解mongodb,并注意到db.stats()显示的集合数量比db.getCollectionNames()显示的数量多1个

以下是示例:

> db.stats();
{   
  "db" : "learn",
  "collections" : 6,
  "objects" : 47, 
  ...
  ...

  "ok" : 1 
}   

> db.getCollectionNames();
[ "hit_stats", "hits", "system.indexes", "system.profile", "unicorns" ]

所以db.stats表示 6个集合,其中db.getCollectionNames仅列出 5个集合名称。为什么会出现这种差异?

1 个答案:

答案 0 :(得分:2)

您会看到这种差异,因为存储集合信息的system.namespaces集合不包含自身。示例包含2个集合(itemsuser):

> db.system.namespaces.find()
{ "name" : "test.system.indexes" }
{ "name" : "test.items.$_id_" }      // index
{ "name" : "test.items" }
{ "name" : "test.users.$_id_" }      // index
{ "name" : "test.users" }

> db.stats()['collections']
4

> db.getCollectionNames()
[ "items", "system.indexes", "users" ]

正如您所看到的,system.namespaces不包括在内。 db.stats()确实在计算中包含了它,因此收集了1个差异。希望这是有道理的。

另请参阅这些错误报告SERVER-1162SERVER-1308

相关问题