MongoDB: cursor.toArray() method
cursor.toArray
The cursor.toArray() method is used to return an array that contains all documents returned by the cursor.
Syntax:
cursor.toArray()
A collection prod_master contain the following documents.
{ "_id" : 5, "item" : { "name" : "mango", "type" : "cortland" }, "cost" : 1.29 }
{ "_id" : 9, "item" : { "name" : "mango", "type" : "fuji" }, "cost" : 1.99 }
{ "_id" : 7, "item" : { "name" : "apple", "type" : "honey crisp" }, "cost" : 1.99 }
{ "_id" : 10, "item" : { "name" : "mango", "type" : "jonagold" }, "cost" : 1.29 }
{ "_id" : 1, "item" : { "name" : "banana", "type" : "jonathan" }, "cost" : 1.29 }
{ "_id" : 6, "item" : { "name" : "apple", "type" : "mcintosh" }, "cost" : 1.29 }
{ "_id" : 8, "item" : { "name" : "orange", "type" : "cara cara" }, "cost" : 2.99 }
{ "_id" : 4, "item" : { "name" : "apple", "type" : "navel" }, "cost" : 1.39 }
{ "_id" : 3, "item" : { "name" : "orange", "type" : "satsuma" }, "cost" : 1.99 }
{ "_id" : 2, "item" : { "name" : "banana", "type" : "valencia" }, "cost" : 0.99 }
Example: MongoDB: cursor.toArray() method
The following example holds the array of documents returned by toArray().
db.prod_master.find().toArray();
Output:
> db.prod_master.find().toArray();
[
{
"_id" : 5,
"item" : {
"name" : "mango",
"type" : "cortland"
},
"cost" : 1.29
},
{
"_id" : 9,
"item" : {
"name" : "mango",
"type" : "fuji"
},
"cost" : 1.99
},
{
"_id" : 7,
"item" : {
"name" : "apple",
"type" : "honey crisp"
},
"cost" : 1.99
},
{
"_id" : 10,
"item" : {
"name" : "mango",
"type" : "jonagold"
},
"cost" : 1.29
},
{
"_id" : 1,
"item" : {
"name" : "banana",
"type" : "jonathan"
},
"cost" : 1.29
},
{
"_id" : 6,
"item" : {
"name" : "apple",
"type" : "mcintosh"
},
"cost" : 1.29
},
{
"_id" : 8,
"item" : {
"name" : "orange",
"type" : "cara cara"
},
"cost" : 2.99
},
{
"_id" : 4,
"item" : {
"name" : "apple",
"type" : "navel"
},
"cost" : 1.39
},
{
"_id" : 3,
"item" : {
"name" : "orange",
"type" : "satsuma"
},
"cost" : 1.99
},
{
"_id" : 2,
"item" : {
"name" : "banana",
"type" : "valencia"
},
"cost" : 0.99
}
]
Retrieve the restaurants data from here
Previous:
cursor.sort() method
Next:
Mongodb Database db.commandHelp() method
