如何在MongoDB Shell中列出所有集合? [英] How can I list all collections in the MongoDB shell?

查看:364
本文介绍了如何在MongoDB Shell中列出所有集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoDB Shell中,如何列出我正在使用的当前数据库的所有集合?

In the MongoDB shell, how do I list all collections for the current database that I'm using?

推荐答案

您可以...

JavaScript(shell):

JavaScript (shell):

db.getCollectionNames()

Node.js:

db.listCollections()

非JavaScript(仅适用于外壳):

Non-JavaScript (shell only):

show collections

之所以称其为非JavaScript,是因为:

The reason I call that non-JavaScript is because:

$ mongo prodmongo/app --eval "show collections"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
2016-10-26T19:34:34.886-0400 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell eval):1:5

$ mongo prodmongo/app --eval "db.getCollectionNames()"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
[
    "Profiles",
    "Unit_Info"
]

如果您真的想要甜美,甜美的show collections输出,则可以:

If you really want that sweet, sweet show collections output, you can:

$ mongo prodmongo/app --eval "db.getCollectionNames().join('\n')"
MongoDB shell version: 3.2.10
connecting to: prodmongo/app
Profiles
Unit_Info

这篇关于如何在MongoDB Shell中列出所有集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆