2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Sheets with FerretDB and MongoDB
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								sidebar_label: FerretDB / MongoDB
							 
						 
					
						
							
								
									
										
										
										
											2024-03-18 08:24:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								pagination_prev: demos/cli/index
							 
						 
					
						
							
								
									
										
										
										
											2023-02-28 11:40:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								pagination_next: demos/local/index
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								sidebar_custom_props:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  type: document
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-27 09:12:19 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import current from '/version.js';
							 
						 
					
						
							
								
									
										
										
										
											2023-04-29 11:21:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import CodeBlock from '@theme/CodeBlock';
							 
						 
					
						
							
								
									
										
										
										
											2023-04-27 09:12:19 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[MongoDB ](https://mongodb.github.io/node-mongodb-native/ ) is a document-oriented
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								database engine. [FerretDB ](https://www.ferretdb.com/ ) is a truly open source
							 
						 
					
						
							
								
									
										
										
										
											2025-05-27 22:50:10 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								implementation of the MongoDB wire protocol.
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-12-05 03:46:54 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:::note Tested Deployments
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-12-05 03:46:54 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This demo was tested in the following environments:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								| Server              | Connector Library    | Date       |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								|:--------------------|:---------------------|:-----------|
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| FerretDB `1.24.0`    | `mongodb`  (`6.12.0`) | 2025-01-03 |
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								| MongoDB CE `8.0.4`   | `mongodb`  (`6.12.0`) | 2025-01-19 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| MongoDB CE `7.0.16`  | `mongodb`  (`6.12.0`) | 2025-01-19 |
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								| MongoDB CE `6.0.20`  | `mongodb`  (`6.5.0`)  | 2025-01-19 |
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:::
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Integration Details
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								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.
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Importing Data
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Data stored in an array of objects can be added to MongoDB Collections using
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`Collection#insertMany` [^1]. The SheetJS `sheet_to_json`  method[^2] can generate 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								data from worksheets:
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* import data from a worksheet to a collection */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const aoo = XLSX.utils.sheet_to_json(ws);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								await collection.insertMany(aoo, {ordered: true});
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Typically worksheet objects are extracted from workbook objects[^3] generated
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								from the SheetJS `read`  or `readFile`  methods[^4].
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								#### Exporting Data
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-12-22 04:47:57 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`Collection#find` [^5] can pull an array of objects from a MongoDB Collection. 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The SheetJS `json_to_sheet`  method[^6] can take the result and generate a
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								worksheet object.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:::info pass
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Normally the method adds a `_id`  field to each object.  The recommended way to
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								remove the field is to use a `projection`  to suppress the ID.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:::
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```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);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								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].
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Complete Example
  
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-01 10:44:10 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								0) Install a MongoDB-compatible server. Options include MongoDB CE[^9] and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								FerretDB[^10]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1) Start a server on `localhost`  (follow official instructions).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-08 04:47:04 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< details >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < summary > < b > MongoDB CE Setup< / b >  (click to show)< / summary > 
							 
						 
					
						
							
								
									
										
										
										
											2024-04-01 10:44:10 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For MongoDB 8.0 Community Edition, the macOS steps required `brew` :
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								brew tap mongodb/brew
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								brew update
							 
						 
					
						
							
								
									
										
										
										
											2023-08-20 20:39:35 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								brew install mongodb-community
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-05-04 16:15:00 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Older versions can be installed by passing the version major and minor numbers:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Install 7.0
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								brew install mongodb-community@7.0
							 
						 
					
						
							
								
									
										
										
										
											2024-05-04 16:15:00 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-08-20 20:39:35 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If `brew`  was used to install MongoDB, the following command starts a server:
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$(brew --prefix)/opt/mongodb-community/bin/mongod --config $(brew --prefix)/etc/mongod.conf
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-01 10:44:10 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / details >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< details >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < summary > < b > FerretDB Setup< / b >  (click to show)< / summary > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The official documentation recommends Docker, but it is strongly recommended to
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								use [`colima` ](https://github.com/abiosoft/colima ) on MacOS:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								brew install colima docker docker-compose
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To properly install `docker-compose` , the `colima`  process must be run once:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$(brew --prefix)/opt/colima/bin/colima start -f
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								After stopping the process, delete any containers:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$(brew --prefix)/opt/colima/bin/colima delete
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`config.json`  must be edited: 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Homebrew docker plugins folder must be added to `cliPluginsExtraDirs` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  All `credsStore`  settings must be removed. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When the demo was last tested, the following `config.json`  was used:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```json title="~/.docker/config.json"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "auths": {},
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "currentContext": "colima",
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* highlight-start */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "cliPluginsExtraDirs": [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    "/opt/homebrew/lib/docker/cli-plugins"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  /* highlight-end */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:::note pass
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `/opt/homebrew`  part of the path must be replaced with the Homebrew prefix.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following command displays the prefix:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								brew --prefix
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								After making the change, start `colima`  again and keep the window open:
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$(brew --prefix)/opt/colima/bin/colima start -f
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Start the FerretDB server in a new terminal window:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								cd /tmp
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								docker run -d --rm --name ferretdb -p 27017:27017 -e FERRETDB_HANDLER=sqlite ghcr.io/ferretdb/all-in-one
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / details >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								2) Create base project and install the dependencies:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-29 11:21:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< CodeBlock  language = "bash" > {`\ 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								mkdir sheetjs-mongo
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								cd sheetjs-mongo
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								npm init -y
							 
						 
					
						
							
								
									
										
										
										
											2025-01-06 02:51:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								npm i --save https://cdn.sheetjs.com/xlsx-${current}/xlsx-${current}.tgz mongodb@6.12.0`}
							 
						 
					
						
							
								
									
										
										
										
											2023-04-29 11:21:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< / CodeBlock >  
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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 */
							 
						 
					
						
							
								
									
										
										
										
											2025-01-23 18:52:41 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const client = await MongoClient.connect(url, {});
							 
						 
					
						
							
								
									
										
										
										
											2023-02-24 07:46:48 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								/* 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 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-10-13 10:04:30 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								4) Run the script:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								node SheetJSMongoCRUD.mjs
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								There should be no errors in the terminal. The script will generate the file
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`SheetJSMongoCRUD.xlsx` . That file can be opened in a spreadsheet editor. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^1]: See [`insertMany` ](https://mongodb.github.io/node-mongodb-native/5.7/classes/Collection.html#insertMany ) in the MongoDB documentation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^2]: See [`sheet_to_json` in "Utilities" ](/docs/api/utilities/array#array-output )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^3]: See ["Workbook Object" ](/docs/csf/book )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^4]: See [`read` and `readFile` in "Reading Files" ](/docs/api/parse-options )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^5]: See [`find` ](https://mongodb.github.io/node-mongodb-native/5.7/classes/Collection.html#find ) in the MongoDB documentation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^6]: See [`json_to_sheet` in "Utilities" ](/docs/api/utilities/array#array-of-objects-input )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^7]: See ["Workbook Helpers" in "Utilities" ](/docs/api/utilities/wb ) for details on `book_new`  and `book_append_sheet` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^8]: See [`writeFile` in "Writing Files" ](/docs/api/write-options )
							 
						 
					
						
							
								
									
										
										
										
											2024-04-01 10:44:10 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[^9]: See ["Install MongoDB Community Edition" ](https://www.mongodb.com/docs/manual/administration/install-community/#std-label-install-mdb-community-edition ) in the MongoDB documentation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[^10]: See ["SQLite Setup with Docker Compose" ](https://docs.ferretdb.io/quickstart-guide/docker/#sqlite-setup-with-docker-compose ) in the FerretDB documentation.