Complete MongoDB Tutorial #6 - Adding New Documents
Summary
TLDRIn this informative tutorial, viewers learn how to interact with MongoDB databases using both the MongoDB shell and the GUI tool, Compass. The instructor demonstrates switching databases, referencing collections, and inserting documents into a collection using the 'insert one' method for single documents and the 'insert many' method for multiple documents. MongoDB automatically generates a unique ID for each document. The video also shows how documents can be added to a non-existent collection, which MongoDB will create upon insertion. By the end of the lesson, viewers are equipped with the skills to manage documents in MongoDB effectively.
Takeaways
- ๐ป Use MongoDB shell and Compass to interact with databases.
- ๐ Switch to the correct database using `use bookstore`.
- ๐ Reference a collection with `db.collectionName`.
- ๐ `insertOne` method is used to add a single document to a collection.
- ๐ Properties of a book document include title, author, pages, genres, and rating.
- ๐ MongoDB automatically assigns a unique ID to each document.
- ๐ `insertMany` method allows inserting multiple documents at once.
- ๐๏ธ You can insert a document into a non-existing collection, and MongoDB will create it.
- ๐ Verify document insertion using Compass to see the updated collection.
- ๐๏ธ Use `db.collection.drop()` to delete a collection if it's no longer needed.
Q & A
How do you access the MongoDB shell?
-You can enter the MongoDB shell by using the command line and typing `mongo`.
What is the purpose of MongoDB Compass?
-MongoDB Compass is a GUI tool used to visualize and interact with your MongoDB data.
How do you switch databases in the MongoDB shell?
-You switch databases by using the `use` command followed by the name of the database, such as `use bookstore`.
How do you reference a collection in MongoDB?
-You reference a collection by using `db` followed by the collection name, like `db.books`.
What is the `insertOne` method used for in MongoDB?
-The `insertOne` method is used to insert a single document into a collection.
What properties does a book document typically have in the provided script?
-A book document typically has properties like title, author, pages, genres (an array), and rating.
Does MongoDB automatically assign an ID to a document?
-Yes, MongoDB automatically assigns a unique ID to a document when it is created.
Can you insert a document into a collection that does not exist?
-Yes, if you try to insert a document into a non-existing collection, MongoDB will create the collection for you.
How do you delete a document in MongoDB?
-While the script does not explicitly mention document deletion, you can delete a document using the `deleteOne` or `deleteMany` methods.
What is the `insertMany` method used for in MongoDB?
-The `insertMany` method is used to insert multiple documents into a collection at once.
How can you verify that documents have been added to a collection?
-You can verify documents have been added by using MongoDB Compass to view the collection and its documents.
Outlines
๐ Adding Documents to MongoDB using Shell
The paragraph explains how to use MongoDB shell to add documents to a collection. It starts with instructions on switching to the correct database (bookstore) from the test database. The narrator then shows how to reference the 'books' collection and use the 'insertOne' method to add a single document. The document is created with properties like title, author, pages, genres, and rating, without needing an ID as MongoDB auto-generates one. The result of the operation is shown, and the newly added document is verified in MongoDB Compass.
๐ Inserting Multiple Documents at Once
This paragraph demonstrates how to insert multiple documents into a MongoDB collection at once using the 'insertMany' method. After showing that it's unnecessary to have a collection pre-existing to insert documents, the narrator proceeds to use the 'insertMany' method on the 'books' collection. Documents are added by passing an array of objects, each representing a document. The operation's success is confirmed by checking the response object for acknowledged status and the generated document IDs. The addition of documents is then verified in MongoDB Compass, showing an increased count of documents in the collection.
Mindmap
Keywords
๐กMongoDB
๐กMongoDB Shell
๐กMongoDB Compass
๐กDatabase
๐กCollection
๐กDocument
๐กInsert One
๐กInsert Many
๐กProperties
๐กGenres
๐กObjectId
Highlights
Introduction to using MongoDB shell and Compass for database interaction.
Demonstration of switching databases in MongoDB using the 'use' command.
Explanation of referencing a collection in MongoDB.
How to insert a single document into a collection using the 'insertOne' method.
Details of the properties of a book document in MongoDB.
MongoDB automatically assigns a unique ID to new documents.
Adding properties to a document such as title, author, pages, genres, and rating.
Confirmation of document insertion and viewing the new document's ID.
Verification of document insertion in Compass and the automatic creation of the ID property.
Ability to insert a document into a non-existing collection, which will create the collection.
Example of inserting a document into a non-existing 'authors' collection.
Instructions on how to delete a document and drop a collection in MongoDB.
Explanation of inserting multiple documents at once using the 'insertMany' method.
Demonstration of copying and pasting multiple document objects for insertion.
Confirmation of multiple document insertion and viewing their IDs.
Verification of multiple document insertion in Compass.
Transcripts
all right then gang so we've seen in the
last couple of lessons how to enter into
the mongodb shell to communicate with
our databases and also how to use
mongodb compass the gui tool to see the
data in our databases so going forward
we're going to use a combination of
these two tools the shell and also
compass
now at the moment we can see inside
compost that we have a database called
bookstore and inside that a books
collection and in the books collection
we have a few bulk documents we added
those documents through compass a couple
of lessons ago so now i want to show you
how to add documents to a collection
from the shell as well so the first
thing we need to do is make sure we're
in the correct database now currently
we're in the test database so we want to
switch to the bookstore so we can add
those book documents so i'm going to say
use and then bookstore
which is the name of the database we
want to use and now we're switched to
that all right so once we're in the
bookstore we first of all need to
reference a collection in order to put a
document inside it right now to do that
we say db which is the current database
that we're in that references the
database remember if we press enter we
can see that that's bookstore so we say
db and then dots and then whatever the
collection is called now we created a
collection called books like so so we
can say db.box and that would reference
essentially the box collection now if i
press enter we can see that it's
bookstore.books so i'm going to say
db.books
and then we can use a method on this
called insert
one
so this function right here is going to
insert a single document into the box
collection and inside the parentheses
right here we pass an argument and that
argument is essentially an object which
represents the book that we want to add
so remember our books have a few
different properties let's just take a
look at what those properties were
they have a title an author pages and
genres which is an array of different
genres and also a rating okay so let's
now add these different properties
inside
the object that we just created we don't
have to add the id by the way mongodb
automatically assigns a unique id when
we create a document so let's go back
over here and let's first of all add the
title property so i'll say title and i'm
just going to say
the color
of
magic like so
we also need an author which is terry
pratchett
so terry
pratchett i think that's how you spell
his name not 100
and then after that we need the number
of pages now i'm just going to guess i'm
going to select 300.
we also need after that the rating which
i'm going to give a seven after the
rating we also need a genres property
so let's do that as well and this is an
array i'm gonna put in here
fantasy and also magic i know that's not
a real genre but whatever for me is all
right so i think that's pretty much it
we have the title the author the pages
the rating and the genres and all of
these properties go inside this object
right here that we passed into the
insert one method right so when we do
that it's going to interact with our
books collection to add this new
document and mongodb is also going to
auto create the id property so press
enter and now we can see this response
right here so this is an object we get
back to say it's acknowledged our
request and the id of the document is
just inserted so it's created this
object id and applied it to the document
we've just created
now if i go back to
compass over here then
if i refresh this we can see now we have
five documents and if i scroll down i
should see the color of magic as well
awesome
so it's added that document for us we've
added a single new document using this
method which was
insert one all right and by the way
you don't need a collection to exist for
you to insert a new document so
for example i could say dp and then
authors which is the authors collection
that doesn't exist at the minute if we
take a look inside compass we only have
the books collection right this one
right here so that doesn't exist yet but
it doesn't matter i can still insert one
like so
and let's just say i want to give this a
name property and the name is going to
be
brandon
sanderson like so
and let's also say an age property
really don't know how old brandon
sanderson is i'm going to guess at 60.
press enter now it's still going to
insert this document even though the
author's collection doesn't exist and i
can verify that by going over here
and then go into the bookstore and if
i refresh over here
then we can see the new authors
collection as well all right so if we go
into that we can see now we have a
document inside that collection now i
actually don't want this collection so
i'm going to delete this and also over
here i'm going to drop the collection
because i don't want the author's
collection so let me just copy authors
right here
and paste it in and drop that collection
so now we only have the one collection
again i just wanted to show you that we
don't actually have to have a collection
already existing for you to insert a
document into it if there's not a
collection already called authors when
we try to insert a document it will
create it for us okay
so
that's how to insert a single document
using this method insert one but we can
also insert many documents at once
and to do that we just use a different
method so this time we say db again to
go into the database and then after that
we say the collection name which is
going to be books and then we want to
use the method insert
many so we use this function if we want
to insert more than one document at once
and then inside here we just pass
through an array of different objects
where each object represents a single
document we want to add now instead of
writing these out from scratch i'm going
to copy and paste them from my notepad
woohoo oops we've lost the terminal let
me open that back up again so i'm going
to grab those and i'm going to paste
them inside here so we have two
documents right here we have this one
then a comma and then the next document
as well so the first one is the like
fantastic and the second one is june so
if we press enter now hopefully it will
add both of those documents and we can
see acknowledged is true and we can see
the ids that it has inserted for both of
these documents and we can verify
they've been added by going to compass
then going into this collection
and if we refresh over here then we
should see seven documents and we do
let's try and find them should be near
the bottom the like fantastic and also
june awesome so there we go my friends
that's how we can add documents to our
database using the shell we have those
two methods insert one to insert a
single document and also insert many to
insert many documents which are inside
an array
5.0 / 5 (0 votes)