MongoDB is a NoSQL database which works on document oriented storage. It doesn’t support traditional SQL joins but uses a concept known as referencing and embedded documents instead.
Here’s an example of how to establish a reference relationship:
1. Create two collections, one called “authors” and the other “books”.
1. When you add data to “authors”, MongoDB will automatically assign a unique “\_id” for each author.
```
db.authors.insert({
name: ‘George R. R. Martin’,
age: 71
})
```
1. Then when you add data to “books”, you can include the authors’ “\_id” as a reference.
```
db.books.insert({
title: ‘A Game of Thrones’,
author_id: ObjectId(“…”)
})
```
1. You can then use this reference to lookup related data using `$lookup` operator provided by MongoDB.
```
db.books.aggregate([{
$lookup: {
from: ‘authors’,
localField: ‘author_id’,
foreignField: ‘_id’,
as: ‘author‘
}
}])
```
This technique is called “manual references”. However, if you had a one-to-one or one-to-few relationship you might consider using an “embedded document” where you could put all the data relating to one “book” (including the author) into a single document in MongoDB.