我在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集合名。为什么会有这种差异?
发布于 2013-02-03 06:08:36
您可以看到这种差异,因为存储集合信息的system.namespaces集合本身并不包括在内。示例包含2个集合(items和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集合的差异。希望这是有意义的。
https://stackoverflow.com/questions/14651497
复制相似问题