- Reference >
mongoshMethods >- Cursor Methods >
- cursor.map()
cursor.map()¶
-
cursor.map(function)¶ Important
mongoshMethodThis 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 :driver:`MongoDB driver documentation </>`.
For the legacy
mongoshell documentation, refer to the documentation for the corresponding MongoDB Server release:Applies a
functionto each document visited by the cursor and collects the return values from successive applications of thefunctioninto aCursorobject.The
cursor.map()method has the following parameter:Parameter Type Description functionfunction A function to apply to each document visited by the cursor.
Behavior¶
cursor.map() returns a Cursor object. Note that
.map() only converts the type, it does not create a new cursor. You
can convert the Cursor object to an Array with .toArray().
Examples¶
These examples refer to the products collection:
Return a Value From a Collection¶
Get the product names.
Return Results as an Array¶
Calculate a discounted sale price and return the results as an array.
Confirm that the output is an Array
See also
cursor.forEach() for similar functionality.