
1. Overview
In this post, we will learn to fix “SyntaxError: Missing semicolon” when trying to export or dump MongoDB collections using the mongo shell.
2. SyntaxError: Missing semicolon Mongodb
Whenever you face this issue, verify the following points:
2.1. Run MongoDB commands from the system shell
You must run mongoexport
, mongodump
, mongoimport
, mongorestore
, bsondump
from the System shell directly.
These commands do not work with?mongosh
shell.
You must download the?mongo-database-tools
to use these commands. The MongoDB Database Tools are a collection of command-line utilities for working with a MongoDB deployment. These tools release independently enabling us to receive more frequent updates and leverage new features as soon as they are available.?
?The Database Tools include the following binaries:
Command | Description |
---|---|
Binary Import / Export | |
????mongodump | Creates a binary export of the contents of a?mongod ?database. |
????mongorestore | Restores data from a?mongodump ?database dump into a?mongod ?or?mongos |
????bsondump | Converts?BSON?dump files into?JSON. |
Data Import / Export | |
????mongoimport | Imports content from an?Extended JSON, CSV, or TSV export file. |
????mongoexport | Produces a?JSON?or?CSV?export of data stored in a?mongod ?instance. |
Diagnostic Tools | |
????mongostat | Provides a quick overview of the status of a currently running?mongod ?or?mongos ?instance. |
????mongotop | Summarizes the time a?mongod ?instance spends reading and writing data. |
GridFS Tools | |
????mongofiles | Supports manipulating files stored in your MongoDB instance in?GridFS?objects. |
Starting with MongoDB 4.4, the MongoDB Database Tools are now released separately from the MongoDB Server and use their own versioning, with an initial version of?100.0.0
. Previously, these tools were released alongside the MongoDB Server and used matching versioning.
If you are seeing the below error while running the above MongoDB commands, then those database binaries are not in your System PATH or not installed in your machine.
zsh: command not found: mongodump
You can directly download the database tools from MongoDB website or you can install the database tools using homebrew:
brew install mongodb/brew/mongodb-database-tools
Of course, make sure you have homebrew already installed.
Then the following command should work from your system shell to perform an export:
mongodump --uri="mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mongodb.net/sample_supplies"
2.2. Check the command format carefully.
If you still face issues, validate the command that you are using:
- You must use double hyphens — in front of the command-line options. Example:
--uri
to specify the MongoDB URI. - You must specify value after = equals operator of a command line option. Example:
--uri="mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mongodb.net/sample_supplies"
- Check whether you are using a valid URI connection string, username, and password. You can refer to this link to know the format of the connection string.
3. Conclusion
To sum up, we have learned to fix the SyntaxError: Missing semicolon while using MongoDB utility DB commands. To learn more about MongoDB, refer to these articles.