MongoDB 中 db.collection.find() 的返回类型是什么?
语句 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" }
广告