要获取文档,请find()
在MongoDB中使用。这样,可以格式化结果文档pretty()
。
让我们创建一个包含文档的集合-
> db.demo306.insertOne({"Name":"Robert","Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea7c6f8647eb59e562038") } > db.demo306.insertOne({"Name":"David","Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea7cdf8647eb59e562039") } > db.demo306.insertOne({"Name":"Mike","Age":25}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea7d4f8647eb59e56203a") } > db.demo306.insertOne({"Name":"Carol","Age":26}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ea7dcf8647eb59e56203b") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo306.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5e4ea7c6f8647eb59e562038"), "Name" : "Robert", "Age" : 21 } { "_id" : ObjectId("5e4ea7cdf8647eb59e562039"), "Name" : "David", "Age" : 23 } { "_id" : ObjectId("5e4ea7d4f8647eb59e56203a"), "Name" : "Mike", "Age" : 25 } { "_id" : ObjectId("5e4ea7dcf8647eb59e56203b"), "Name" : "Carol", "Age" : 26 }