Can I "use" into sub-collections/contexts?

In the course we were told to do the following:

use video
db.moviesScratch.insertOne({title: "Star Trek II: The Wrath of Khan", year: 1982, imdb: "tt0084726"})

So I tried doing this:

use video
use moviesScratch
db.insertOne({title: "Star Trek II: The Wrath of Khan", year: 1982, imdb: "tt0084726"})

And it threw an error that insertOne can’t be done with “db”. I then tried doing this (after the above):

db.moviesScratch.insertOne({title: "Star Trek II: The Wrath of Khan", year: 1982, imdb: "tt0084726"})

It ended up creating a new top-level “collection” (?) next to my admin, local, and video ones. So “show dbs” displays this:

admin          0.000GB
local          1.850GB
moviesScratch  0.000GB
video          0.001GB

I also tried “use video.moviesScratch” and it threw an error.

I don’t understand how this works. Why would “db.moviesScratch” refer to the top-level if I wrote “use” twice?

Can someone explain what’s going on, and if what I was trying to do is possible?

Hi Aaron_Franke_59358,

When you use a command like:

  use moviesScratch

The Mongo shell is connected to Database: moviesScratch and it creates a database if it does not exists. So whatever operations you perform from there on, it will be related to database: moviesScratch; unless you switch the database. The below link will help you.

https://docs.mongodb.com/manual/core/databases-and-collections/index.html

Kanika

1 Like

When you typed “use video” it switched to the “video” database.
Then when you typed “use moviesScratch”, it created a new database called “moviesScratch” and switched to that.
At that point, you had a new (empty) database called “moviesScratch” but it wouldn’t have showed up if you had typed “show dbs”.

>
> use video
switched to db video
>
> show collections
movieDetails
>
> use moviesScratch
switched to db moviesScratch
>
> // In which database am I currently?
> db
moviesScratch
>
> show dbs
local  0.000GB
test   0.000GB
video  0.001GB
>

Then you typed an “insertOne” function with incorrect syntax. You must mention the collection name between “db” and “insertOne”. Like so:
db.<collection_name>.insertOne()

The keyword “db” remains fixed though! You entered the database when you typed “use <database_name>”. So “db” means “my current database”.
Think of it like this:

Database ==> Collection ==> Document
Database is a bunch of Collections. Collections are "inside" a database.
Collection is a bunch of Documents. Documents are "inside" a collection.

Then you typed the “db.moviesScratch.insertOne()” command. The syntax was correct.
But since you mentioned “moviesScratch” between “db” and “insertOne”, MongoDB created a collection called “moviesScratch” for you. And that is in the database called “moviesScratch”, since you specified that in the “use moviesScratch” command before it.

> // Wrong syntax; no collection specified between "db" and "insertOne"
> db.insertOne({title: "Star Trek II: The Wrath of Khan", year: 1982, imdb: "tt0084726"})
2019-01-22T14:10:46.195-0500 E QUERY    [thread1] TypeError: db.insertOne is not a function :
@(shell):1:1

>
> // Show all collections in the current database (nothing to show)
> show collections
>
> // And show my current db
> db
moviesScratch
>
> // Since you specifed "moviesScratch", that was the name of the newly created collection!
> db.moviesScratch.insertOne({title: "Star Trek II: The Wrath of Khan", year: 1982, imdb: "tt0084726"})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5c476bfcc302e68ace2db1a9")
}
> 
> // Show all collections in the current db
> show collections
moviesScratch
>
> db
moviesScratch
>
> db.moviesScratch.find().pretty()
{
        "_id" : ObjectId("5c476bfcc302e68ace2db1a9"),
        "title" : "Star Trek II: The Wrath of Khan",
        "year" : 1982,
        "imdb" : "tt0084726"
}
>
> // Show all databases
> show dbs
local          0.000GB
moviesScratch  0.000GB
test           0.000GB
video          0.001GB
>

Regarding your question: it doesn’t matter how many times you type “use”. Every time you type "use ", it will switch to a database called . If it does not exist, it will create an empty one and switch to it.

>
> show dbs
local  0.000GB
test   0.000GB
video  0.001GB
>
>
> // Switch to existing database called "video"
> use video
switched to db video
>
> // Create a database called "foo" and switch to it
> use foo
switched to db foo
>
> // But it won't show up in the output of "show dbs"
> show dbs
local  0.000GB
test   0.000GB
video  0.001GB
>
> // Create a database called "bar" and switch to it
> use bar
switched to db bar
>
> // Create a database called "baz" and switch to it
> use baz
switched to db baz
>
>

Finally, if you inadvertently create such databases and want to get rid of them then switch to them and then type “db.dropDatabase()”.

(Disclaimer: Test it on Development and make sure you understand it completely before trying it on Production!)

>
> // Switch to moviesScratch database
> use moviesScratch
switched to db moviesScratch
>
> // drop all collections in "moviesScratch" database
> db.dropDatabase()
{ "dropped" : "moviesScratch", "ok" : 1 }
>
> // I am still on an empty database called "moviesScratch" so it will show up below
> db
moviesScratch
>
> // But no more collections
> show collections
>
> // And since the database is empty, it won't show up below
> show dbs
local  0.000GB
test   0.000GB
video  0.001GB
>

Hope that helps.

2 Likes

Everytime when you type command with Use name . It will automatically creates a New database.
In order to insert data into a collection :
1)You must first get into Database by using command use DatabaseName
2)After, then you can insert data into collection in that database by the following way :
db.collectionName.insert()
In simple words collection is nothing but a Table in which you are trying to insert data.

So is there no short-hand way to do it? I can’t have MongoDB remember which collection I’m working on for later?

You can specify which database to select when running mongo command like this:

  mongo videos

It will select videos database when you connect.

Kanika

You could assign the collection name to a variable and then use that variable from anywhere.
The Mongo shell is a Javascript shell, so Javascript variable assignment works.

>
> use video
switched to db video
>
> show collections
movieDetails
>
> db.movieDetails.count()
2295
>
> // assign my collection to a variable so I can use it from wherever I want
> var col = db.movieDetails
>
> col
video.movieDetails
>
> col.count()
2295
>
>
> show dbs
local  0.000GB
test   0.000GB
video  0.001GB
>
> use test
switched to db test
>
> show collections
test
>
> // Now I switched to another db but my "col" variable still points to video.movieDetails
> col
video.movieDetails
>
> // So I can use all collection methods on it (from any database)
> col.count()
2295
>
>

Not sure if that’s what you were looking for.

1 Like

Thank you! This isn’t exactly what I was thinking but it’s the next best thing.