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.