MongoDB Shell SyntaxError: unexpected token: string literal

I’m trying to connect to the sandbox for the MongoDB University into course, but keep getting syntax errors when I use the copied command from Atlas to connect.

MongoDB Shell version 4.2.3


Last time I connected to the Atlas cluster I used something like this from Mongo Shell (and it worked):

mongo mongodb+srv://

Note that there are no quotes around the connect string.


Hi @Lillie_Sauer welcome to the community!

You are trying to connect to your MongoDB instance from a shell that is already connected.

The MongoDB Enterprise > bit is the MongoDB shell prompt. Your operating system prompt would normally end with a $ or #.

The SyntaxError bit you’re getting is the mongo shell not being able to interpret what you’re trying to say.

Try typing in list databases or list collections and you will see the databases in your system or the collections in your current database.


Yes, the MongoDB Enterprise > is the Mongo Shell prompt. So, as @Doug_Duncan mentioned you have to enter the connection string (e.g., mongo mongodb+srv:// from the operating system command prompt.

The Mongo Shell utility program allows you connect to the MongoDB database server using a connection string, e.g., just typing mongo from OS command prompt will connect to the MongoDB server at the default localhost at port 27017. In the screen shot @Lillie_Sauer had posted, is already connected to the server perhaps using the default host and port.

Actually, from the Mongo Shell prompt, the commands are show dbs (prints all the database names on the MongoDB server) and show collections (prints all the collection names in the current database).

1 Like

@Prasad_Saya you are correct. The shell commands are indeed show dbs and show collections. I got the list and show confused because you can also run db.adminCommand( { listDatabases: 1 } ) and db.runCommand( { listCollections: 1.0 } ) to get similar information.

@Doug_Duncan, @Prasad_Saya
Thanks for your help! That makes sense. However, is there a way to disconnect from a database inside the mongo shell? Or does it stay connected until you connect to a new shell through the command prompt?

The shell will stay connected to the database while it is open. You can connect to another database from the same shell using the command use database_name. This will change your context to the database named database_name. If you don’t supply a database when connecting with the mongo shell you will connect to the test database by default. You can see your current database connection, while in the mongo shell by typing db.

> db
> use testing
switched to db testing
> db

If you are in the Mongo Shell and want to exit it you can type:
MongoDB Docs

1 Like

As, @Doug_Duncan mentioned you can switch between databases on the same server. To quit the Mongo Shell, use quit() or CTRL+C.

You can connect to multiple shells at any given time. Each shell is started from an individual command window. This allows multiple tasks, like be connected to multiple databases on your server, or altogether multiple MongoDB servers (e.g., one local and one remote).

More at Mongo Shell Quick Reference.

@Lillie_Sauer If you really want to disconnect from the db you can start in nodb mode and create a connection object yourself using Mongo() .

You can also connect to multiple mongodb clusters in the same shell.

// Connect to a cluster
c0 = Mongo("mongodb://")
// Get DB in connection string
d0 = c0.getDB(c0.defaultDB)
// Get another db on the cluster
d0a = c0.getDB('admin')

// Connect to another cluster
c1 = Mongo("mongodb://")

// Close connections

You would think that using db.getMongo().close() would work. It does not appear to actually disconnect the socket. Using my example I actually see the connection close in the mongod logs.