This content originally appeared on DEV Community and was authored by The Nerdy Dev
Hey guys 👋🏻,
In this article, let us understand about The NoSQL Database : Basics of CRUD in MongoDB. So in this article we will learn about how to retrieve to retrieve/access, delete, create and update our documents. We will also see how we can delete a document by making use of its id and all that jazz. We will be covering the basics about collections and documents, the basic data types and we will also see how we can perform these CRUD operations. So without a further ado, let's get started.
This article was first published on https://the-nerdy-dev.com/.
Check the website for more interesting articles and tutorials on Web Development.
Database, Collections and Documents
In the MongoDB world, you can have one or more databases. Each database can hold one or more collections. Now what is a collection ?
A collection is just like a table that we have in a relational database like SQL. In a collection that you and I create, we can have multiple documents. Something that we will discuss a bit later.
Now what do we mean by documents ?
A document is essentially a data piece that you store in the database.
You don't need to worry about these much. When working with MongoDB database, collections and documents gets implicitly created for you when you start storing the data and work with them.
A bit later in this article we will also learn an explicit way of creating documents in your collection using which you can also configure them a bit further.
The Shell and the MongoDB drivers for different languages
You will use MongoDB drivers in the real world applications
depending on the programming language you are using for writing
the backend servers on.
Shell is the only approach that will work for every language.
MongoDB CRUD Operations
Shell is what gets connected to the local mongo server.
Creating Databases and Collections
To check what databases, we have on the server we can run the command :
show dbs
Now to switch to a database, we can make use of the use
command.
If the database exists, then the use
command in that case will make use of the existing one otherwise it will create the new one on the fly so basically on the execution of the command. So let us create a database and call it as flights
So to switch to the flights
database, we can say :
use flights
We store collections in database. But in order to access a document we need to access that respective collection first in which the document resides, then only we will be able to access the document.
Understanding JSON data
Every document you enter gets a new unique id which is the feature
of the MongoDB. You need to have a unique id for each and every document. We do not have to specify it on its own. Mongodb does this for us under the hood which is of type ObjectId
which is another type supported by MongoDB. This id will allow you to sort the documents because it has some timestamp associated to it. This is how JSON data gets stored into our database, to be precise into the collection of our database.
To list all the databases that are there on the MongoDB server, we can run the command
show dbs
and this would give us something like this :
admin 0.000GB
config 0.000GB
local 0.000GB
shop 0.000GB
Now let us switch to the flights
database. Now currently as you can see, the flights
database does not exists. So to create it on the fly, we can make use of the command use flights
and this will automatically generate a flights
database for us.
use flights
As a result of the above command, we get this as output in our terminal :
switched to db flights
Let us insert a new flight into the flightData
collection which gets implicitly created on demand. Now this new flight is a new document which gets inserted in the flightData
collection of the flights
database which we reference as db
. So db
here represents the current database to which we are connected to.
Now on running the above query, we get this as the output :
{
"acknowledged" : true,
"insertedId" : ObjectId("5bad0895316926267806295d")
}
To display the array of flight documents in a pretty formatted manner.
- First we reference the current active database
db
. - Then reference the collection whose all the documents we want to retrieve which in our case is
flightData
- The
find()
method retrieves all the documents in the current referenced collection.
So this is our query for same :
and this is the output for same :
{
"_id" : ObjectId("5bad0895316926267806295d"),
"departureAirport" : "MUC",
"arrivalAirport" : "SFO",
"aircraft" : "Airbus A380",
"distance" : 12000,
"intercontinental" : true
}
Let us now do a comparison between JSON and BSON.
Comparing JSON and BSON
This is what JSON looks like :
{
"name": "Alex",
"age": 27
}
JSON is what we insert or retrieve. Behind the scenes, MongoDB actually uses BSON data. This conversion is done by the drivers. This is simply done because BSON is more effective
to store than the JSON data, faster and more efficient in terms of size and efficient storage. There are different types of data - numbers, binaries etc. which are stored in different ways behind the scenes
BSON stands for Binary JSON for storing data into the database.
Let us insert one more flight in our flightData
collection.
{
"acknowledged" : true,
"insertedId" : ObjectId("5bad13cd316926267806295e")
}
{
"_id" : ObjectId("5bad0895316926267806295d"),
"departureAirport" : "MUC",
"arrivalAirport" : "SFO",
"aircraft" : "Airbus A380",
"distance" : 12000,
"intercontinental" : true
}
{
"_id" : ObjectId("5bad13cd316926267806295e"),
"departureAirport" : "TXL",
"arrivalAirport" : "LHR"
}
This above insertOne
does work because the collection
can accept mixed documents since we know MongoDB is schemaless.
Two documents in the same collection don't necessarily need to have the same schema
We may have the schema but it is not the must.
The id which is autogenerated you just don't have to use the autogenerated id you just have to ensure that you have a unique id but if you cannot ensure this you can assign the ids on your own.
Let us insert the same document which we inserted which had missing schema with the id of our choice.
Make sure the id that you use is unique.
So here is the query for same. Please note here we are using an id of our choice i.e lmn-kpq:
After inserting the document into the flightData
collection, we can find all the documents within the collection
{
"_id" : ObjectId("5bad0895316926267806295d"),
"departureAirport" : "MUC",
"arrivalAirport" : "SFO",
"aircraft" : "Airbus A380",
"distance" : 12000,
"intercontinental" : true
}
{
"_id" : ObjectId("5bad13cd316926267806295e"),
"departureAirport" : "TXL",
"arrivalAirport" : "LHR"
}
{ "_id" : "txl-lhr", "departureAirport" : "TXL", "arrivalAirport" : "LHR" }
Here the id is unique and it is the one that we used while creating the new document. It doesn't have ObjectId
type
for the simple reason because it was not autogenerated by MongoDB it is the id that we gave to the document. In the next article, we will learn more about CRUD operations. See you next time !
Thanks for reading.
If you enjoy my articles, consider following me on Twitter for more interesting stuff :
⚡Twitter : https://twitter.com/The_Nerdy_Dev
Don't forget to leave a like if you loved the article. Also share it with your friends and colleagues.
PS - If you are looking to learn Web Development, I have curated a FREE course for you on my YouTube Channel, check the below article :
(2021) - Web Developer Full Course : HTML, CSS, JavaScript, Node.js and MongoDB
The Nerdy Dev ・ Apr 28 ・ 2 min read
This content originally appeared on DEV Community and was authored by The Nerdy Dev
The Nerdy Dev | Sciencx (2021-12-07T16:22:00+00:00) The NoSQL Database : Basics of CRUD in MongoDB – Part 1 – insertOne, find. Retrieved from https://www.scien.cx/2021/12/07/the-nosql-database-basics-of-crud-in-mongodb-part-1-insertone-find/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.