128 lines
3.4 KiB
Markdown
128 lines
3.4 KiB
Markdown
|
---
|
||
|
title: MongoDB
|
||
|
pagination_prev: demos/grid
|
||
|
pagination_next: demos/worker
|
||
|
sidebar_custom_props:
|
||
|
type: document
|
||
|
---
|
||
|
|
||
|
MongoDB is a popular document-oriented database engine.
|
||
|
|
||
|
It is straightforward to treat collections as worksheets. Each object maps to
|
||
|
a row in the table.
|
||
|
|
||
|
## Integration Details
|
||
|
|
||
|
The official NodeJS connector is `mongodb`.
|
||
|
|
||
|
#### Importing Data
|
||
|
|
||
|
Data stored in an array of objects can be added to MongoDB Collections using
|
||
|
`Collection#insertMany`. `sheet_to_json` 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});
|
||
|
```
|
||
|
#### Exporting Data
|
||
|
|
||
|
`Collection#find` can pull an array of objects from a Mongo Collection.
|
||
|
|
||
|
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();
|
||
|
```
|
||
|
|
||
|
Worksheets can be generated from the result using `json_to_sheet`:
|
||
|
|
||
|
```js
|
||
|
/* generate a worksheet from a collection */
|
||
|
const aoo = await collection.find({}, {projection:{_id:0}}).toArray();
|
||
|
const ws = utils.json_to_sheet(aoo);
|
||
|
```
|
||
|
|
||
|
## Complete Example
|
||
|
|
||
|
:::note
|
||
|
|
||
|
This demo was last tested on 2023 February 23 with MongoDB CE 6.0.4, MongoDB
|
||
|
connector module 5.1.0 and NodeJS 18.14.2.
|
||
|
|
||
|
:::
|
||
|
|
||
|
0) Install MongoDB 6.0 Community Edition. The macOS steps required `brew`:
|
||
|
|
||
|
```bash
|
||
|
brew tap mongodb/brew
|
||
|
brew update
|
||
|
brew install mongodb-community@6.0
|
||
|
```
|
||
|
|
||
|
1) Start a MongoDB server on `localhost` (follow official instructions). To run
|
||
|
in the foreground on Intel MacOS:
|
||
|
|
||
|
```bash
|
||
|
/usr/local/opt/mongodb-community/bin/mongod --config /usr/local/etc/mongod.conf
|
||
|
```
|
||
|
|
||
|
2) Create base project and install the dependencies:
|
||
|
|
||
|
```bash
|
||
|
mkdir sheetjs-mongo
|
||
|
cd sheetjs-mongo
|
||
|
npm init -y
|
||
|
npm i --save https://cdn.sheetjs.com/xlsx-latest/xlsx-latest.tgz mongodb@5.1.0
|
||
|
```
|
||
|
|
||
|
3) Save the following to `SheetJSMongoCRUD.mjs` (the key step is highlighted):
|
||
|
|
||
|
```js title="SheetJSMongoCRUD.mjs"
|
||
|
import { writeFile, set_fs, utils } from 'xlsx';
|
||
|
import * as fs from 'fs'; set_fs(fs);
|
||
|
import { MongoClient } from 'mongodb';
|
||
|
|
||
|
const url = 'mongodb://localhost:27017/sheetjs';
|
||
|
const db_name = 'sheetjs';
|
||
|
|
||
|
/* Connect to mongodb server */
|
||
|
const client = await MongoClient.connect(url, { useUnifiedTopology: true });
|
||
|
|
||
|
/* Sample data table */
|
||
|
const db = client.db(db_name);
|
||
|
try { await db.collection('pres').drop(); } catch(e) {}
|
||
|
const pres = db.collection('pres');
|
||
|
await pres.insertMany([
|
||
|
{ name: "Barack Obama", idx: 44 },
|
||
|
{ name: "Donald Trump", idx: 45 },
|
||
|
{ name: "Joseph Biden", idx: 46 }
|
||
|
], {ordered: true});
|
||
|
|
||
|
// highlight-start
|
||
|
/* Create worksheet from collection */
|
||
|
const aoo = await pres.find({}, {projection:{_id:0}}).toArray();
|
||
|
const ws = utils.json_to_sheet(aoo);
|
||
|
// highlight-end
|
||
|
|
||
|
/* Export to XLSX */
|
||
|
const wb = utils.book_new();
|
||
|
utils.book_append_sheet(wb, ws, "Presidents");
|
||
|
writeFile(wb, "SheetJSMongoCRUD.xlsx");
|
||
|
|
||
|
/* Close connection */
|
||
|
client.close();
|
||
|
```
|
||
|
|
||
|
This script:
|
||
|
|
||
|
- connects to the local MongoDB server using database `sheetjs`
|
||
|
- removes the `pres` collection if it already exists
|
||
|
- creates a new collection `pres` with sample data
|
||
|
- creates a SheetJS worksheet from the collection (highlighted in the snippet)
|
||
|
- creates a SheetJS workbook, adds the worksheet, and exports to XLSX
|
||
|
|
||
|
4) Run `node SheetJSMongoCRUD.mjs` and open `SheetJSMongoCRUD.xlsx`
|