Home > OS >  Mongo shell throws "ReferenceError db is not defined" even when I am connected and using t
Mongo shell throws "ReferenceError db is not defined" even when I am connected and using t

Time:10-14


nicole@MacBook-Air client % mongosh
Current Mongosh Log ID: 6167b26073f2085eee4554b6
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:          5.0.3
Using Mongosh:          1.1.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting:
   2021-10-13T15:36:00.307-07:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------

test> show dbs
admin     41 kB
config   111 kB
local     41 kB
test    8.19 kB
test> use test
already on db test
test> show collections
users
test> test.users.find()
ReferenceError: test is not defined
test> users.find()
ReferenceError: users is not defined
test> 

I'm new to mongo, and I just installed a local db on my Mac (Big Sur 11.4).

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

brew tap mongodb/brew
brew install [email protected]
brew services start [email protected]

I just made a users collections via an express server using mongoose. I then opened up the database in my terminal to inspect the contents. However, I get a very strange error that tells me the database test is not defined, even though the shell tells me I'm using the test database. Is also says the collection users is not defined, but it is there??

What am I doing wrong?

CodePudding user response:

Your command is wrong. Use one of these:

use test
db.users.find()
db.getCollection("users").find()

db.getSiblingDB("test").users.find()
db.getSiblingDB("test").getCollection("users").find()
  • Related