w3resource

MongoDB: cursor.forEach() method

cursor.forEach

The cursor.forEach() method is used to apply a JavaScript function for each document in a cursor.

The forEach() method has the following prototype form:

db.collection.find().forEach(<function>)

Syntax:

cursor.forEach(function)

Parameters:

Name Description Required /
Optional
Type
function A JavaScript function to apply to each document from the cursor. The <function> signature includes a single argument that is passed the current document to process. Required JavaScript

Sample document in the restaurants collection:


{
  "address": {
     "building": "1007",
     "coord": [ -73.856077, 40.848447 ],
     "street": "Morris Park Ave",
     "zipcode": "10462"
  },
  "borough": "Bronx",
  "cuisine": "Bakery",
  "grades": [
     { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
     { "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
     { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
     { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
     { "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
  ],
  "name": "Morris Park Bake Shop",
  "restaurant_id": "30075445"
}
.....

Example: MongoDB: cursor.forEach() method

The following example invokes theforEach()method on the cursor returned byfind()to print the name of each restaurant in the collection.

db.restaurants.find().forEach( function(myDoc) { print( "name: " + myDoc.name ); } );

Output:

.....
name: Hang # 16
name: Lowkey Lounge Inc.
name: Cannelle Lic
name: Matcha Cafe Wabi
name: New King Work Ny Inc
name: Teng Da
name: Ocean Xi Lounge Karaoke Inc.
name: Malt And Mold
name: King Garden
name: Vegetarian Choice Restaurant
name: Jimbo'S Hamburgers
name: Bombay Grill
name: Curry Garden
name: Asian Time Eatery
name: Hollis-Pj Inc
name: Hibachi Dumpling
name: Sarku Japan Teriyaki And Sushi Express
name: Tbaar
name: Lao Ma Spicy
name: Tbaars
name: Tacombi At Ganesvoort Market
name: Fouta Halal Food Corp.
name: El Grand Chef Restaurant & Pizzeria
name: Buttercup Bake Shop
name: Bagatelle Kiss & Fly
name: The Milton
name: Bluestone Lane Coffee
name: Beyond Sushi Nyc Inc
name: Incredibowl
name: Lui'S Sweet Tomatoes Bakery
name: Ny Fortune Garden Inc
name: Dunkin Donuts
name: Steam It Corp
name: Dou Man Jiang Bbq Restaurant
name: Crustacean Nyc
name: Fat Cats Pizza
name: Adam Koshary & Grill
name: Nueva Nitin Bakery
name: Piura
name: The Taco Place Inc
name: A+ Chinese Restaurant
name: Caffe Bean
name: Hua Rong
name: Lucky Vegetarian
name: Lyric Theatre
name: Two Boots Park Slope
name: Long Wong Bakery Ii, Inc.
name: Plant Love House
name: Jimbo'S
name: Mr. Tony Pizzeria
name: Jimbo'S Hamburger
name: Columbia University Medical Center Bookstore Cafe
name: The Waylon
name: Juquila Mexican Cuisine
name: Culture 36
name: Bubble Panda
name: Joy Burger Bar
name: Montague St Bagels
name: Papa Johns Pizza/Lawrence Fried Chicken
name: Almando Restaurant Inc
name: Lola Lola Restaurant & Bar
name: Berti Restaurant & Lounge
name: Rose Pizza
name: Juice Press
name: San Gennaro
name: Wibar
name: Comfort Diner
name: Kabirs Bakery
name: Bocca Bliss
name: Kababish
name: China Green Dim Sum Restaurant
name: Subway
name: New Mombar Inc
name: Forcella Pizza Napoli
.....	

Retrieve the restaurants data from here

Previous: cursor.explain() method
Next: cursor.hasNext() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/shell-methods/cursor/cursor-forEach.php