---
title: Sheets with MongoDB
sidebar_label: MongoDB / FerretDB
pagination_prev: demos/cli/index
pagination_next: demos/local/index
sidebar_custom_props:
  type: document
---
import current from '/version.js';
import CodeBlock from '@theme/CodeBlock';
[MongoDB](https://mongodb.github.io/node-mongodb-native/) is a document-oriented
database engine.
[SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing
data from spreadsheets.
This demo uses SheetJS to exchange data between spreadsheets and MongoDB. We'll
explore how to use save tables from a MongoDB collection to spreadsheets and how
to add data from spreadsheets into a collection.
:::note Tested Deployments
This demo was tested in the following environments:
| Server              | Connector Library   | Date       |
|:--------------------|:--------------------|:-----------|
| FerretDB `1.21.0`   | `mongodb` (`5.9.2`) | 2024-03-30 |
| MongoDB CE `6.0.10` | `mongodb` (`5.7.0`) | 2023-12-04 |
| MongoDB CE `7.0.2`  | `mongodb` (`5.7.0`) | 2023-12-04 |
:::
## Integration Details
The [SheetJS NodeJS module](/docs/getting-started/installation/nodejs) can be
loaded in NodeJS scripts that use the `mongodb` NodeJS connector library.
It is straightforward to treat collections as worksheets.  Each object maps to
a row in the table.
#### Importing Data
Data stored in an array of objects can be added to MongoDB Collections using
`Collection#insertMany`[^1]. The SheetJS `sheet_to_json` method[^2] can generate
data from worksheets:
```js
/* import data from a worksheet to a collection */
const aoo = XLSX.utils.sheet_to_json(ws);
await collection.insertMany(aoo, {ordered: true});
```
Typically worksheet objects are extracted from workbook objects[^3] generated
from the SheetJS `read` or `readFile` methods[^4].
#### Exporting Data
`Collection#find`[^5] can pull an array of objects from a Mongo Collection.
The SheetJS `json_to_sheet` method[^6] can take the result and generate a
worksheet object.
:::info pass
Normally the method adds a `_id` field to each object.  The recommended way to
remove the field is to use a `projection` to suppress the ID.
:::
```js
/* generate an array of objects from a collection */
const aoo = await collection.find({}, {projection:{_id:0}}).toArray();
/* generate a worksheet from a collection */
const ws = utils.json_to_sheet(aoo);
```
Using `book_new` and `book_append_sheet`[^7], a workbook object can be created.
This workbook is typically exported to the filesystem with `writeFile`[^8].
## Complete Example
0) Install a MongoDB-compatible server. Options include MongoDB CE[^9] and
FerretDB[^10]
1) Start a server on `localhost` (follow official instructions).
MongoDB CE Setup (click to show)
For MongoDB 7.0 Community Edition, the macOS steps required `brew`:
```bash
brew tap mongodb/brew
brew update
brew install mongodb-community
```
:::note pass
If `brew` was used to install MongoDB, the following command starts a server:
```bash
/usr/local/opt/mongodb-community/bin/mongod --config /usr/local/etc/mongod.conf
```
If Homebrew is configured to use `/opt/homebrew`, the command is:
```bash
/opt/homebrew/opt/mongodb-community/bin/mongod --config /opt/homebrew/etc/mongod.conf
```
:::