How to show all databases in mongoshell? - mongodb

I apologize if this is easily google-able or already asked before.
I have tried googling, but I get hits to explain how to get all collections.
I am using MongoDB 2.4.8

Run show databases or show dbs in the Mongo shell:
$ mongo
MongoDB shell version: 2.4.8
connecting to: test
> show databases
db1 0.203125GB
local 0.078125GB
test 0.203125GB
This will list your databases.
To list the available databases, use the helper show dbs. See also How can I access different databases temporarily? to access a different database from the current database without switching your current database context (i.e. db..) source

Related

Why doesn't my created DB show up when I doe show databases in mongo? [duplicate]

This question already has an answer here:
Mongo: show dbs doesn't show the test db. Why?
(1 answer)
Closed 3 years ago.
I tried going into a mongo shell and creating a DB, and it seems to have worked, its saying I'm in the db i created but when I run show databases; it doesnt show in the list.
Here is what terminal shows:
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
> use sr-dev
switched to db sr-dev
> show databases;
admin 0.000GB
config 0.000GB
local 0.000GB
> db
sr-dev
>
Mongo only creates databases and collections as needed, as lazily as possible. You didn't "create" sr-dev, you're merely "using" it at the moment. Think of it as "being in the namespace sr-dev". Once you insert your first data the actual database will be created. Before you do that, there's no need for it to exist.
Create a Database
If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in the mongo shell: [..]
https://docs.mongodb.com/manual/core/databases-and-collections/#create-a-database
When you create a database in MongoDb shell, it won't show up until there are some collections in your database.
Please try adding some collections in your newly created database and run show databases command again, It will show up.
db.createCollection('collection_name',{...})
Here the 2nd parameter is optional.
Please read MongoDb createCollection documentation for more info, on 2nd parameter options.
You will have to create collection inside your db.
MongoDB works this way.

how to show my users collection with mongodb

I'm sorry for this (peraphs) stupid question ... I install meteor and mongodb in my windows computer and i start to write some apps. I don't understand how to use mongo for shoving my db app ... i open one shell in my app dir and launch mongod, in one more shell in the same folder i start mongo.
show dbs
local
use local
switched to db local
show collections
startup_log
system.indexes
Where are my collections? Where is users collection?
When your app is running use this command on a separate command line mongo 127.0.0.1:3001
Meteor keeps the collections in this server. After you run mongo on this server, by writing use meteor you can use db specific to your running app. And then you can display your collections with db.getCollectionNames()
Meteor uses a library called Minimongo that's why it doesn't display if you run show dbs on your mongo shell.
By default it points to port 3001 hence if you are using Robomongo you can just make the set up to watch that port.
To display all your MongoDB collections using the shell, you may check this answer:
How to list all collections in the mongo shell?
You may also use a MongoDB GUI Tool such Robomongo

Mongodb : why show dbs does not show my databases?

I have setup mongodb 64bits on Windows. I ran server and client successfully.
But when I type:
show dbs
Output is
local 0.000GB
Why ? show dbs is supposed to list all databases at least the default one "test"
am I wrong ?
Although you may be in the test database by default, the database does not actually get created until you insert a document into a collection in the database which will implicitly create the collection and the database.

mongo shell unusable, but access from my program is ok, after uninstalling/new install on OSX

all!
After uninstalling current stable (with brew), and installing the 2.4 rc2 (not brew, since brew --devel does not have it, but mainly as described here: http://shiftcommathree.com/articles/how-to-install-mongodb-on-os-x), even a couple of times, and perhaps being too eager to delete everything mongodb-related before the next install, I have ended up with this situation:
The Node-program I have that uses the database and works ok (find, insert and so on). But the problem is that the mongo shell does not work properly, I cannot insert or find documents. For example (now I have 2.4 rc2, but the situation was the same with the current stable):
$ mongo
MongoDB shell version: 2.4.0-rc2
connecting to: test
> show dbs
local 0.078125GB
> use mydb
switched to db mydb
> db.mydb.mycollection.insert = ({title: "O-oh, strange problem"})
{ "title" : "O-oh, strange problem" }
> show collections
> show dbs
local 0.078125GB
mydb (empty)
Then I do a insert via my Node-program and do this:
> show dbs
local 0.078125GB
mydb 0.203125GB
> use mydb
switched to db mydb
> show collections
mycollection
system.indexes
> db.mydb.mycollection.find()
>
Again, in my Node-program, 'find' finds what it should...
Any idea why mongo shell does not work?
Frode
You've got a collection called mydb.mycollection in a mydb database. It is unlikely that's really what you want. When you use a database from the shell, you are then able to reference the database via the db object without repeating the database name.
use mydb
# db is now implicitly the mydb database
# you no longer refer to it by name
db.mycollection.insert({"name" : "stacks overflowing"})
Also note that you were not calling the insert function, your code was attempting to set the function to the value of the new object.

Some beginner's questions about MongoDB

I'm a beginner with MongoDB and I've some questions:
When I'm connected to Mongo, and i execute show dbs I see 2 databases: admin and local. What's their role? Then if I execute an insert command like db.foo.insert({"value":"mongo"}), the test database appears. Why? How can i specify a custom name for a database?
With show dbs I get the databases (somehow like show databases in sql), how can I then list the collections inside a database (I would use show tables in sql)?
When executing a command, the MongoDB tutorial always uses the db object. Is it the main object (a sort of "connection" object) that has to used for executing commands or it's something else?
Thanks!
admin and local contain various settings local to the server, like users who are authenticated to connect. Under beginner usage, you shouldn't need to worry about them at all. By default you connect to a database named test. To connect to a new database, just use databasename from the mongo command line, or mongo databasename from your OS shell.
use [database_name] and then show collections
The db object is your root handle to the currently-selected database on the mongo commmand line. The command line is really just a Javascript command line, and there are various mongodb-specific objects and functions exposed that let you do stuff. Try help() for a full listing.