Bryson Kruk
Bryson Kruk

Reputation: 281

Confused about databases vs collections

I am new to mongo and currently trying to set up my project's database with the mongo shell. I understand conceptually that a collection is like a table, and databases house collections.

I am confused about the following:

Upvotes: 3

Views: 937

Answers (1)

Nadine
Nadine

Reputation: 1638

is 'db' my database I'm working in? I read that the initial database is 'test' and 'db'. Which one is it?

db refers to the database you are currently using. By default, when you first open mongo, that database is called test.

You can switch to a different database (called test2 for example) like this:

>use test2
switched to db test2

Now db will refer to test2, the new database that you are using.

When I use a command like 'db.bears.find()', Is bears a database or collection? When I use both commands 'show dbs' and 'db.getCollectionNames()', 'bears is listed in both! How can they be collections and databases?

When you write db.bears.find() bears in that command is a collection. If bears is appearing in both show dbs and db.getCollectionNames() that means you have a collection called bears and a database called bears. They are two separate entities.

Is nested databases possible?

If I understand your question correctly, no it's not possible.

Upvotes: 3

Related Questions