1

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

这是示例:

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

  "ok" : 1 
}   

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

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

4

1 回答 1

2

您会看到这种差异,因为system.namespaces存储集合信息的集合不包括其自身。items带有 2 个集合(和)的示例user

> 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

于 2013-02-03T06:08:36.663 回答