cursor.toArray()

cursor.toArray()
Important重要
mongosh Method

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

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

←  cursor.tailable()Database Methods →