Examples
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.join[];
Try it Yourself »
Another separator:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let text = fruits.join[" and "];
Try it Yourself »
Definition and Usage
The join[]
method returns an array as a string.
The join[]
method does not change the original array.
Any separator can be specified. The default is comma [,].
Syntax
Parameters
Parameter | Description |
separator | Optional. The separator to be used. Default is a comma. |
Return Value
Type | Description |
A string. | The array values, separated by the specified separator. |
Browser Support
join[]
is an ECMAScript1 [ES1] feature.
ES1 [JavaScript 1997] is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
SQL JOIN
A JOIN
clause is used to combine rows from two or more tables, based on a related column between them.
Let's look at a selection from the "Orders" table:
10308 | 2 | 1996-09-18 |
10309 | 37 | 1996-09-19 |
10310 | 77 | 1996-09-20 |
Then, look at a selection from the "Customers" table:
1 | Alfreds Futterkiste | Maria Anders | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mexico |
Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.
Then, we can create the following SQL statement [that contains an
INNER JOIN
], that selects records that have matching values in both tables:
Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON
Orders.CustomerID=Customers.CustomerID;
Try it Yourself »
and it will produce something like this:
10308 | Ana Trujillo Emparedados y helados | 9/18/1996 |
10365 | Antonio Moreno Taquería | 11/27/1996 |
10383 | Around the Horn | 12/16/1996 |
10355 | Around the Horn | 11/15/1996 |
10278 | Berglunds snabbköp | 8/12/1996 |
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
[INNER] JOIN
: Returns records that have matching values in both tablesLEFT [OUTER] JOIN
: Returns all records from the left table, and the matched records from the right tableRIGHT [OUTER] JOIN
: Returns all records from the right table, and the matched records from the left tableFULL [OUTER] JOIN
: Returns all records when there is a match in either left or right table
Join Collections
MongoDB is not a relational database, but you can perform a left outer join by using the $lookup
stage.
The $lookup
stage lets you specify which collection you want to join with the current collection, and which fields that should match.
Consider you have a "orders" collection and a "products" collection:
orders
[
{ _id: 1, product_id: 154, status: 1 }
]
products
[
{
_id: 154, name: 'Chocolate Heaven' },
{ _id: 155, name: 'Tasty Lemons' },
{ _id: 156, name: 'Vanilla Dreams' }
]
Example
Join the matching "products" document[s] to the "orders" collection:
var MongoClient = require['mongodb'].MongoClient;
var url = "mongodb://127.0.0.1:27017/";
MongoClient.connect[url, function[err, db] {
if [err] throw err;
var dbo = db.db["mydb"];
dbo.collection['orders'].aggregate[[
{ $lookup:
{
from: 'products',
localField: 'product_id',
foreignField: '_id',
as: 'orderdetails'
}
}
]].toArray[function[err, res] {
if [err] throw err;
console.log[JSON.stringify[res]];
db.close[];
}];
}];
Run example »
Save the code above in a file called "demo_mongodb_join.js" and run the file:
Run "demo_mongodb_join.js"
C:\Users\Your Name>node demo_mongodb_join.js
Which will give you this result:
[
{ "_id": 1, "product_id": 154, "status": 1, "orderdetails": [
{ "_id": 154, "name": "Chocolate Heaven" } ]
}
]
As you can see from the result above, the matching document from the products collection is included in the orders collection as an array.