MongoDB #2 – CRUD

Table of contents
Reading Time: 3 minutes

Hello everyone! In my previous blog I explained about what is MongoDB and why we need to go with it you can find in this blog.

https://www.codeproject.com/KB/database/1037052/image006.png

In this blog, I will try to explain CRUD operations on MongoDB using MongoShell.

  • To check available database:-
>show dbs

This command will show you a list of all available database.

one

  • To create a database in MongoDB:- By using the command given below, you can switch to the new database.
>use DATABASE_NAME

Here DATABASE_NAME is representing your database  name which you want to create

  • To check the current database : –
>db
  • To create a collection in database:- In the command given below, db is the reference to the database that you are correctly using.
>db.createCollection("COLLECTION_NAME")

2

  • To check available collection:-
>db.getCollectionNames()

3

  • To Insert a document in a collection:- MongoDB provides the following methods to insert documents into a collection.
  1. To insert one document:-
>db.COLLECTION_NAME.insertOne()

insertOne

 2. To insert many documents:-

>db.COLLECTION_NAME.insertMany()

insertMany

Here you can see we inserted two documents at a time and it returns the acknowledgment of inserted document. You also noticed that it returns _id which is the unique id that is set by MongoDB. You can read more about the primary key(_id) in my previous blog.

  •  To retrieve all data from the collection:-
>db.COLLECTION_NAME.find()

findAll

  • To retrieve the data that satisfy the condition:-
>db.COLLECTION_NAME.find()

In the example given below, the condition applied is: name should be “shubham”

searchOne

  • Retrieve only one document:- Returns one document that satisfies the specified query criteria on the collection or view. If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk
>db.COLLECTION_NAME.findOne()
  • To update a document:-   MongoDB uses the update() method for updating its document that accepts two arguments.

1. A document in which you need to define the condition for updating the document.

2. A document which contains the fields which need to be updated.

>db.COLLECTION_NAME.update()

8

Notice: When the updated condition that meets more than one document then MongoDB updates only one document. For updating all the documents that satisfy the condition, you have to set the value of the field multi as true.

10

  • To remove document in MongoDB: Removes all documents from the collection.
>db.COLLECTION_NAME.remove()
  • To remove document by condition:-
>db.COLLECTION_NAME.remove()

10

Hope this blog helps you to do some basic operations on MongoDB. In my next blog, I will explain more operations like sorting, aggregation etc. so we will meet soon with another blog 🙂

knoldus-advt-sticker

Written by 

Shubham is a Software Consultant, with experience of more than 1.5 years.He is familiar with Object Oriented Programming Paradigms. He is always eager to learn new and advance concepts. Aside from being a programmer, his hobbies include playing badminton,watching movies, writing blogs. He has experience working in C, C++, CoreJava, Adv Java, HTML, CSS, JS, Ajax.

1 thought on “MongoDB #2 – CRUD2 min read

Comments are closed.