Docs HomeMongoDB Manual

cursor.toArray()

cursor.toArray()
Important

mongosh Method

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.

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中并耗尽游标。

Returns:返回值:An array of documents.一组文档。

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()返回的文档数组。