cursor.toArray()

cursor.toArray()

mongo Shell Method

This page documents the mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. 本页记录了mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。

The toArray() method returns an array that contains all the documents from a cursor. 方法返回一个数组,该数组包含游标中的所有文档。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()返回的文档数组。