Md *_*han 7 mongodb mongodb-3.4
在 MongoDB shell 中,如何查看我正在使用的当前数据库的特定集合中的所有文档?
当我尝试查询时
> db.getCollection().find()
Run Code Online (Sandbox Code Playgroud)
要获得如下所述的错误
2017-10-14T00:57:34.363+0530 E QUERY [thread1] Error: collection constructor called with undefined argument :
DB.prototype.getCollection@src/mongo/shell/db.js:34:16
@(shell):1:1
Run Code Online (Sandbox Code Playgroud)
我也在此处上传 mongo shell 命令提示符的屏幕截图
小智 8
好,让我们从基础开始吧!
使用 command 连接到 mongod 后mongo
。
show dbs
iot:PRIMARY> show dbs
admin 0.000GB
iot 0.020GB
local 0.042GB
test 0.000GB
testi 0.000GB
use iot
命令选择数据库之一iot:PRIMARY> use iot
switched to db iot
show collections
命令列出该数据库上的集合iot:PRIMARY> show collections
data
header
key
iot:PRIMARY> db.header.find()
{ "_id" : "1b5caa", "temp1" : "Temperature", "pressure1" : "Pressure", "humidity1" : "Humidity", "uv1" : "UV", "BusV1" : "Solar Panel (V)", "Current1" : "Solar Panel Current (mA)", "BusV2" : "Battery (V)", "Current2" : "Battery Current (mA)" }
{ "_id" : "30444", "temp1" : "Temperature", "pressure1" : "Pressure", "humidity1" : "Humidity" }
{ "_id" : "239684", "temp1" : "Temperature", "pressure1" : "Pressure", "humidity1" : "Humidity" }
因此,您需要使用use
命令连接 WANTED 数据库,并且需要显示要查询的集合db.<collection_name>.find()
如何查看我当前连接的数据库?只需发出命令db
,您就会得到答案,您当前的数据库是什么。
参考:
https://docs.mongodb.com/manual/tutorial/query-documents/
选择集合中的所有文档
要选择集合中的所有文档,请将一个空文档作为查询过滤器参数传递给 find 方法。查询过滤器参数确定选择条件:
db.inventory.find( {} )
Run Code Online (Sandbox Code Playgroud)
这些操作对应于以下 SQL 语句:
SELECT * FROM inventory
Run Code Online (Sandbox Code Playgroud)