语句db.collection.find()返回查询的结果集的游标,您可以通过该游标遍历结果集或打印所有文档。
首先让我们创建一个包含文档的集合-
> db.findCursorDemo.insertOne({"ClientFirstName":"John","ClientLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00a1c588d4a6447b2e05c") } > db.findCursorDemo.insertOne({"ClientFirstName":"Carol","ClientLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00a26588d4a6447b2e05d") } > db.findCursorDemo.insertOne({"ClientFirstName":"David","ClientLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00a33588d4a6447b2e05e") }
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.findCursorDemo.find();
这将产生以下输出-
{ "_id" : ObjectId("5cd00a1c588d4a6447b2e05c"), "ClientFirstName" : "John", "ClientLastName" : "Smith" } { "_id" : ObjectId("5cd00a26588d4a6447b2e05d"), "ClientFirstName" : "Carol", "ClientLastName" : "Taylor" } { "_id" : ObjectId("5cd00a33588d4a6447b2e05e"), "ClientFirstName" : "David", "ClientLastName" : "Miller" }