cursor.toArray()
cursor.toArray()- Important
mongosh Method
This page documents a
mongoshmethod. 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.
ThetoArray()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()返回的文档数组。