Docs Menu
Docs Home
MongoDB Manual
/ / /

On this page

  • Behavior
  • Examples


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.

Applies a function to each document visited by the cursor and collects the return values from successive applications of the function into a Cursor object.

The method has the following parameter:

A function to apply to each document visited by the cursor. 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().

These examples refer to the products collection:

{ _id: 1, name: 'widget', price: 10.89 },
{ _id: 2, name: 'thing', price: 11.24 },
{ _id: 3, name: 'moppet', price: 8 },
{ _id: 4, name: 'cosa', price: 24.19 }

Get the product names.

db.products.find().map( function(p) { return; } ) ;

Calculate a discounted sale price and return the results as an array.

var salePrices = db.products.find().map( function(p) { return p.price * .9 } ).toArray() ;

Confirm that the output is an Array


See also:

cursor.forEach() for similar functionality.

← cursor.limit()

On this page