0

Hello I've been using mongodb with mongo shell and I keep having the same problem. While the database called Videos exists in MongoDB Compass and I can use it and add data to it, when I try to access it from the mongosh with this command:

Videos.getCollectionNames();

or with this command:

Videos.GBvideos.find();

I keep getting this error:

ReferenceError: Videos is not defined

This happens both with the integrated mongosh (inside the Compass env.) and with the mongosh executable from the installation path.

Any ideas how to get over this error?

I need to use the shell as what I want to do can't be done with the Compass

Compass: This is what I see from Compass

whatevahhh
  • 47
  • 6
  • if you execute "show dbs" , "use Videos;db.GBvideos.find();" ? – R2D2 Jan 24 '22 at 18:32
  • show dbs returns `Videos 88.2 MB admin 41 kB config 127 kB local 73.7 kB` use Videos;db.GBvideos.find(); returns: `MongoshInvalidInputError: [COMMON-10001] Invalid database name: Videos;db.GBvideos.find()` – whatevahhh Jan 24 '22 at 18:33
  • 1
    mongos> use Videos switched to db Videos mongos> db.GBvideos.find(); mongos> – R2D2 Jan 24 '22 at 18:37
  • `use Videos switched to db Videos` worked for me thank you so much! – whatevahhh Jan 24 '22 at 18:39

2 Answers2

1

use Videos switched to db Videos is the way to change the database from the mongo shell kudos to R2D2!!

whatevahhh
  • 47
  • 6
1

You can do this way in single command:

   mongos> db.getSiblingDB("Videos").getCollection("GBVideos").find()
   { "_id" : ObjectId("61eef2439f3fb15d1fef8877"), "test" : 1 }
   { "_id" : ObjectId("61eef24e9f3fb15d1fef8878"), "test" : 1 }
   mongos> 

Or if you need your way you must assingn to variable:

   mongos> var Videos=db.getSiblingDB("Videos")
   mongos> Videos.GBVideos.find()
   { "_id" : ObjectId("61eef2439f3fb15d1fef8877"), "test" : 1 }
   { "_id" : ObjectId("61eef24e9f3fb15d1fef8878"), "test" : 1 }
   mongos> 
R2D2
  • 9,410
  • 2
  • 12
  • 28