Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started With MongoDB (Part 2)

DZone's Guide to

Getting Started With MongoDB (Part 2)

Learn how to perform various CRUD operations in MongoDB using MongoShell.

· Database Zone ·
Free Resource

Download the Altoros NoSQL Performance Benchmark 2018. Compare top NoSQL solutions – Couchbase Server v5.5, MongoDB v3.6, and DataStax Enterprise v6 (Cassandra).

Hello everyone! In my previous article, I explained what MongoDB is and why you should use it. In this article, I will try to explain CRUD operations in MongoDB using MongoShell.

To check available databases:

>show dbs

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

one

To create a database in MongoDB:

>use DATABASE_NAME

Here, DATABASE_NAME represents the name of the database that you want to create.

To check the current database:

>db

To create a collection in database: 

>db.createCollection("COLLECTION_NAME")

Image title

Here, db refers to the database that you are using.

To check available collections:

>db.getCollectionNames()

3

To insert a document in a collection, there are a few options.

  1. To insert one document:
>db.COLLECTION_NAME.insertOne()

insertOne

2. To insert many documents:

>db.COLLECTION_NAME.insertMany()

insertMany

Here, you can see that we inserted two documents at a time and the acknowledgment of the inserted document was returned. You also notice 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 article.

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:

>db.COLLECTION_NAME.findOne()

This 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 that reflects the order of documents on the disk.

To update a document (MongoDB uses the update() method for updating a document that accepts two arguments):

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

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

>db.COLLECTION_NAME.update()

8

Note: When the updated condition meets more than one document, 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:
>db.COLLECTION_NAME.remove()
  • To remove document by condition:
>db.COLLECTION_NAME.remove()

10

I hope this article helps you to do some basic operations on MongoDB. In my next article, I will explain more operations like sorting, aggregation, etc. 

Download the whitepaper, Moving From Relational to NoSQL: How to Get Started. We’ll take you step by step through your first NoSQL project.

Topics:
database ,mongodb ,crud ,mongoshell ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}