MongoDB database foreach Loop traversal usage _mongodb

Source: Internet
Author: User
Tags mongodb

The MongoDB database foreach statement circular traversal function is a very common function.
A foreach loop is used to iterate, and the callback function is allowed to execute once per loop.
In addition, the Foreach Loop traversal is an extension of the For loop, which is consistent with the browser-side foreach usage.

Examples are as follows:

>var arr = ["AB", "CD", "EF"]
>var show = function (Value,index,ar) {print (value)}
>arr.foreach (show)
AB
cd
EF

Additional--A foreach example on the browser side:

Value is the current element values, index is the current element in the array of indices, AR array itself
functionshowresults (value, index, AR) {
document.write ("Value:" + value);
document.write ("index:" + index);
document.write ("Name:" + this.name);
document.write ("
");
}
varletters = [' AB ', ' CD ', ' EF '];
Varscope = {name: ' scope '};
Showresults for the callback function, scope sets the context for the callback so that this point of the callback function points to the scope variable, and the scope is optional
Letters.foreach (showresults,scope);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.