Complete MongoDB Tutorial #6 - Adding New Documents

Net Ninja
23 Mar 202207:16

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

00:00

📚 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.

05:01

📝 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 is a popular NoSQL database that uses a document-oriented storage model. In the context of the video, MongoDB is the primary tool being used to manage and store data. The script references MongoDB as the database system where users can add documents to collections, demonstrating core operations like inserting data.

💡MongoDB Shell

The MongoDB Shell is a command-line interface used to interact with MongoDB databases. The script mentions using the shell to communicate with databases, emphasizing its utility for executing commands such as switching databases and inserting documents.

💡MongoDB Compass

MongoDB Compass is a GUI tool for MongoDB that allows users to visualize and interact with data. The script describes using Compass to view data in databases and to verify changes made via the shell, such as the addition of new documents.

💡Database

A database, in this context, refers to a collection of organized information stored electronically. The script discusses switching to the 'bookstore' database within MongoDB to perform operations, highlighting the concept of databases as containers for data.

💡Collection

In MongoDB, a collection is a group of documents (data records) that typically have similar structures. The script explains how to reference a collection, such as 'books', and how documents are added to it.

💡Document

A document in MongoDB is a basic unit of data, similar to a record in a relational database. The script provides an example of adding a document to the 'books' collection, illustrating the concept of documents as JSON-like objects with various properties.

💡Insert One

The 'insertOne' method is used in MongoDB to add a single document to a collection. The script demonstrates using 'insertOne' to insert a document about 'The Color of Magic', showing how to specify properties like title, author, and rating.

💡Insert Many

The 'insertMany' method allows for the insertion of multiple documents into a collection in a single operation. The script describes using 'insertMany' to add multiple book documents at once, emphasizing efficiency in data entry.

💡Properties

Properties in MongoDB refer to the fields or attributes of a document. The script mentions properties such as 'title', 'author', 'pages', 'genres', and 'rating', which are part of the document structure representing a book.

💡Genres

Genres in the context of the script are categories that classify books, such as 'fantasy' and 'magic'. They are mentioned as part of the properties of a book document, indicating how documents can include arrays or lists of values.

💡ObjectId

ObjectId is a unique identifier that MongoDB automatically assigns to each document. The script explains that when a document is inserted, MongoDB creates an ObjectId for it, ensuring that each document can be uniquely referenced.

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

play00:02

all right then gang so we've seen in the

play00:04

last couple of lessons how to enter into

play00:07

the mongodb shell to communicate with

play00:09

our databases and also how to use

play00:11

mongodb compass the gui tool to see the

play00:14

data in our databases so going forward

play00:17

we're going to use a combination of

play00:18

these two tools the shell and also

play00:21

compass

play00:22

now at the moment we can see inside

play00:24

compost that we have a database called

play00:26

bookstore and inside that a books

play00:28

collection and in the books collection

play00:30

we have a few bulk documents we added

play00:33

those documents through compass a couple

play00:35

of lessons ago so now i want to show you

play00:37

how to add documents to a collection

play00:39

from the shell as well so the first

play00:41

thing we need to do is make sure we're

play00:43

in the correct database now currently

play00:44

we're in the test database so we want to

play00:47

switch to the bookstore so we can add

play00:48

those book documents so i'm going to say

play00:51

use and then bookstore

play00:54

which is the name of the database we

play00:56

want to use and now we're switched to

play00:57

that all right so once we're in the

play00:59

bookstore we first of all need to

play01:01

reference a collection in order to put a

play01:03

document inside it right now to do that

play01:06

we say db which is the current database

play01:08

that we're in that references the

play01:10

database remember if we press enter we

play01:12

can see that that's bookstore so we say

play01:14

db and then dots and then whatever the

play01:17

collection is called now we created a

play01:19

collection called books like so so we

play01:22

can say db.box and that would reference

play01:25

essentially the box collection now if i

play01:27

press enter we can see that it's

play01:29

bookstore.books so i'm going to say

play01:31

db.books

play01:33

and then we can use a method on this

play01:35

called insert

play01:37

one

play01:38

so this function right here is going to

play01:40

insert a single document into the box

play01:43

collection and inside the parentheses

play01:45

right here we pass an argument and that

play01:46

argument is essentially an object which

play01:48

represents the book that we want to add

play01:51

so remember our books have a few

play01:53

different properties let's just take a

play01:54

look at what those properties were

play01:56

they have a title an author pages and

play02:00

genres which is an array of different

play02:02

genres and also a rating okay so let's

play02:07

now add these different properties

play02:09

inside

play02:10

the object that we just created we don't

play02:12

have to add the id by the way mongodb

play02:14

automatically assigns a unique id when

play02:16

we create a document so let's go back

play02:18

over here and let's first of all add the

play02:21

title property so i'll say title and i'm

play02:24

just going to say

play02:26

the color

play02:27

of

play02:28

magic like so

play02:30

we also need an author which is terry

play02:33

pratchett

play02:34

so terry

play02:36

pratchett i think that's how you spell

play02:37

his name not 100

play02:39

and then after that we need the number

play02:41

of pages now i'm just going to guess i'm

play02:43

going to select 300.

play02:44

we also need after that the rating which

play02:48

i'm going to give a seven after the

play02:50

rating we also need a genres property

play02:54

so let's do that as well and this is an

play02:57

array i'm gonna put in here

play02:59

fantasy and also magic i know that's not

play03:01

a real genre but whatever for me is all

play03:04

right so i think that's pretty much it

play03:06

we have the title the author the pages

play03:08

the rating and the genres and all of

play03:11

these properties go inside this object

play03:12

right here that we passed into the

play03:14

insert one method right so when we do

play03:16

that it's going to interact with our

play03:18

books collection to add this new

play03:20

document and mongodb is also going to

play03:23

auto create the id property so press

play03:25

enter and now we can see this response

play03:28

right here so this is an object we get

play03:30

back to say it's acknowledged our

play03:32

request and the id of the document is

play03:36

just inserted so it's created this

play03:38

object id and applied it to the document

play03:40

we've just created

play03:42

now if i go back to

play03:44

compass over here then

play03:47

if i refresh this we can see now we have

play03:50

five documents and if i scroll down i

play03:53

should see the color of magic as well

play03:55

awesome

play03:56

so it's added that document for us we've

play03:58

added a single new document using this

play04:01

method which was

play04:03

insert one all right and by the way

play04:06

you don't need a collection to exist for

play04:10

you to insert a new document so

play04:12

for example i could say dp and then

play04:15

authors which is the authors collection

play04:18

that doesn't exist at the minute if we

play04:19

take a look inside compass we only have

play04:21

the books collection right this one

play04:22

right here so that doesn't exist yet but

play04:25

it doesn't matter i can still insert one

play04:29

like so

play04:30

and let's just say i want to give this a

play04:32

name property and the name is going to

play04:35

be

play04:36

brandon

play04:37

sanderson like so

play04:39

and let's also say an age property

play04:42

really don't know how old brandon

play04:43

sanderson is i'm going to guess at 60.

play04:45

press enter now it's still going to

play04:47

insert this document even though the

play04:49

author's collection doesn't exist and i

play04:51

can verify that by going over here

play04:54

and then go into the bookstore and if

play04:57

i refresh over here

play04:59

then we can see the new authors

play05:01

collection as well all right so if we go

play05:03

into that we can see now we have a

play05:05

document inside that collection now i

play05:07

actually don't want this collection so

play05:09

i'm going to delete this and also over

play05:11

here i'm going to drop the collection

play05:13

because i don't want the author's

play05:15

collection so let me just copy authors

play05:17

right here

play05:18

and paste it in and drop that collection

play05:21

so now we only have the one collection

play05:23

again i just wanted to show you that we

play05:25

don't actually have to have a collection

play05:27

already existing for you to insert a

play05:30

document into it if there's not a

play05:32

collection already called authors when

play05:33

we try to insert a document it will

play05:35

create it for us okay

play05:37

so

play05:38

that's how to insert a single document

play05:40

using this method insert one but we can

play05:43

also insert many documents at once

play05:46

and to do that we just use a different

play05:48

method so this time we say db again to

play05:51

go into the database and then after that

play05:53

we say the collection name which is

play05:55

going to be books and then we want to

play05:57

use the method insert

play05:59

many so we use this function if we want

play06:02

to insert more than one document at once

play06:05

and then inside here we just pass

play06:07

through an array of different objects

play06:09

where each object represents a single

play06:11

document we want to add now instead of

play06:13

writing these out from scratch i'm going

play06:14

to copy and paste them from my notepad

play06:16

woohoo oops we've lost the terminal let

play06:18

me open that back up again so i'm going

play06:21

to grab those and i'm going to paste

play06:23

them inside here so we have two

play06:25

documents right here we have this one

play06:27

then a comma and then the next document

play06:29

as well so the first one is the like

play06:31

fantastic and the second one is june so

play06:33

if we press enter now hopefully it will

play06:35

add both of those documents and we can

play06:37

see acknowledged is true and we can see

play06:40

the ids that it has inserted for both of

play06:43

these documents and we can verify

play06:45

they've been added by going to compass

play06:47

then going into this collection

play06:50

and if we refresh over here then we

play06:52

should see seven documents and we do

play06:54

let's try and find them should be near

play06:56

the bottom the like fantastic and also

play06:59

june awesome so there we go my friends

play07:01

that's how we can add documents to our

play07:04

database using the shell we have those

play07:07

two methods insert one to insert a

play07:09

single document and also insert many to

play07:12

insert many documents which are inside

play07:14

an array

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
MongoDBDatabaseShellCompassInsertDocumentsBookstoreTutorialDataCoding
¿Necesitas un resumen en inglés?