cursor.toArray()
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
The 方法toArray()
method returns an array that contains all the documents from a cursor. toArray()
返回一个数组,其中包含游标中的所有文档。The method iterates completely the cursor, loading all the documents into RAM and exhausting the cursor.该方法完全迭代游标,将所有文档加载到RAM中并耗尽游标。
Consider the following example that applies 考虑以下将toArray()
to the cursor returned from the find()
method:toArray()
应用于find()
方法返回的游标的示例:
var allProductsArray = db.products.find().toArray(); if (allProductsArray.length > 0) { printjson (allProductsArray[0]); }
The variable 变量allProductsArray
holds the array of documents returned by toArray()
.allProductsArray
保存toArray()
返回的文档数组。