Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

MongoDB
Home›Database›MongoDB›Learn MongoDB With Me – Part 2

Learn MongoDB With Me – Part 2

By SibeeshVenu
March 2, 2018
1040
0
Share:

[toc]

Introduction

This is the second article of the series “Learn MongoDB with me”, if you haven’t read my previous post on this topic, I strongly recommend you to find it here. This is the continuation of exploring the Mongo shells, we will be performing some commands on the Mongo shells. For easy reference I will try to add screenshots for each steps I am following. I hope it will help you come along with me. Thanks for reading.

Background

Like I said, it is going to be the second part of the series. I believe that you have enough knowledge about Mongo DB and how to set up it? How a Mongo shells can be used? If you are not able to answer these question yourself, please consider reading my previous posts again.

Mongo shells, the perfect CLI

We can do anything in the Mongo shell, to make the statement clear. I am going to perform some CRUD operations (Create, Read, Update, Delete) within Mongo shells. To do so, we can use Mongo import commands. The Mongo import commands can do the work for you, even if the data is in .tsv, .csv, .json etc. Let’s see those in action.

As a first step, let us see some documentation.

C:\Program Files\MongoDB\Server.4\bin>mongoimport --help

The above command will give you all the options available to get started.

Usage:
mongoimport <options> <file>
Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.
See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information.
general options:
/help                                       print usage
/version                                    print the tool version and
exit
verbosity options:
/v, /verbose:<level>                            more detailed log output
(include multiple times for
more verbosity, e.g. -vvvvv,
or specify a numeric value,
e.g. --verbose=N)
/quiet                                      hide all log output
connection options:
/h, /host:<hostname>                            mongodb host to connect to
(setname/host1,host2 for
replica sets)
/port:<port>                                server port (can also use
--host hostname:port)
kerberos options:
/gssapiServiceName:<service-name>           service name to use when
authenticating using
GSSAPI/Kerberos ('mongodb' by
default)
/gssapiHostName:<host-name>                 hostname to use when
authenticating using
GSSAPI/Kerberos (remote
server's address by default)
ssl options:
/ssl                                        connect to a mongod or mongos
that has ssl enabled
/sslCAFile:<filename>                       the .pem file containing the
root certificate chain from
the certificate authority
/sslPEMKeyFile:<filename>                   the .pem file containing the
certificate and key
/sslPEMKeyPassword:<password>               the password to decrypt the
sslPEMKeyFile, if necessary
/sslCRLFile:<filename>                      the .pem file containing the
certificate revocation list
/sslAllowInvalidCertificates                bypass the validation for
server certificates
/sslAllowInvalidHostnames                   bypass the validation for
server name
/sslFIPSMode                                use FIPS mode of the
installed openssl library
authentication options:
/u, /username:<username>                        username for authentication
/p, /password:<password>                        password for authentication
/authenticationDatabase:<database-name>     database that holds the
user's credentials
/authenticationMechanism:<mechanism>        authentication mechanism to
use
namespace options:
/d, /db:<database-name>                         database to use
/c, /collection:<collection-name>               collection to use
uri options:
/uri:mongodb-uri                            mongodb uri connection string
input options:
/f, /fields:<field>[,<field>]*                  comma separated list of
fields, e.g. -f name,age
/fieldFile:<filename>                       file with field names - 1 per
line
/file:<filename>                            file to import from; if not
specified, stdin is used
/headerline                                 use first line in input
source as the field list (CSV
and TSV only)
/jsonArray                                  treat input source as a JSON
array
/parseGrace:<grace>                         controls behavior when type
coercion fails - one of:
autoCast, skipField, skipRow,
stop (defaults to 'stop')
(default: stop)
/type:<type>                                input format to import: json,
csv, or tsv (defaults to
'json') (default: json)
/columnsHaveTypes                           indicated that the field list
(from --fields, --fieldsFile,
or --headerline) specifies
types; They must be in the
form of
'<colName>.<type>(<arg>)'.
The type can be one of: auto,
binary, bool, date, date_go,
date_ms, date_oracle, double,
int32, int64, string. For
each of the date types, the
argument is a datetime layout
string. For the binary type,
the argument can be one of:
base32, base64, hex. All
other types take an empty
argument. Only valid for CSV
and TSV imports. e.g.
zipcode.string(),
thumbnail.binary(base64)
ingest options:
/drop                                       drop collection before
inserting documents
/ignoreBlanks                               ignore fields with empty
values in CSV and TSV
/maintainInsertionOrder                     insert documents in the order
of their appearance in the
input source
/j, /numInsertionWorkers:<number>               number of insert operations
to run concurrently (defaults
to 1) (default: 1)
/stopOnError                                stop importing at first
insert/upsert error
/mode:[insert|upsert|merge]                 insert: insert only. upsert:
insert or replace existing
documents. merge: insert or
modify existing documents.
defaults to insert
/upsertFields:<field>[,<field>]*            comma-separated fields for
the query part when --mode is
set to upsert or merge
/writeConcern:<write-concern-specifier>     write concern options e.g.
--writeConcern majority,
--writeConcern '{w: 3,
wtimeout: 500, fsync: true,
j: true}'
/bypassDocumentValidation                   bypass document validation

Insert data to MondoDB using Mongo shell

Now let’s say I have a following JSON data, and we are going to insert the same to our db collection.

[ 
{
"color": "black",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,255,255,1],
"hex": "#000"
}
},
{
"color": "white",
"category": "value",
"code": {
"rgba": [0,0,0,1],
"hex": "#FFF"
}
},
{
"color": "red",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,0,0,1],
"hex": "#FF0"
}
},
{
"color": "blue",
"category": "hue",
"type": "primary",
"code": {
"rgba": [0,0,255,1],
"hex": "#00F"
}
},
{
"color": "yellow",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,255,0,1],
"hex": "#FF0"
}
},
{
"color": "green",
"category": "hue",
"type": "secondary",
"code": {
"rgba": [0,255,0,1],
"hex": "#0F0"
}
}
]

To do so, we need to use the following command.

C:\Program Files\MongoDB\Server\3.4\bin>mongoimport --db mylearning --collection colors --jsonArray --file colors.json

Here, as you can see, we are providing the db name, collection, name, what is the data type of the file and finally the file name.

If you ever get the error as “Failed: open colors.json: The system cannot find the file specified.”, please make sure that the document is in the server folder, in my case it is “C:\Program Files\MongoDB\Server\3.4\bin”. If everything is fine, you will be able to see an output as preceding.

C:\Program Files\MongoDB\Server\3.4\bin>mongoimport --db mylearning --collection colors --jsonArray --file colors.json
2018-03-01T16:35:35.470+0530    connected to: localhost
2018-03-01T16:35:36.012+0530    imported 6 documents
C:\Program Files\MongoDB\Server\3.4\bin>

Reading the data from a collection in MongoDB

Now that, we have colors collection, let’s go and check whether the database has the data we are expecting.

C:\Program Files\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2018-03-01T16:24:43.793+0530 I CONTROL  [initandlisten]
2018-03-01T16:24:43.793+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-03-01T16:24:43.793+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-03-01T16:24:43.793+0530 I CONTROL  [initandlisten]
MongoDB Enterprise > use mylearning
switched to db mylearning
MongoDB Enterprise > show collections
chats
colors
messages
MongoDB Enterprise > db.colors.count
function (query, options) {
query = this.find(query);
// Apply options and return the result of the find
return QueryHelpers._applyCountOptions(query, options).count(true);
}
MongoDB Enterprise > db.colors.count()
6
MongoDB Enterprise >

When you use count, make sure you are treating it as a function as count(). We have the count as 6, and that’s what we are expecting. Am I right? Don’t you think, that we should go fetch some data from that collection, yeah with some filter?

MongoDB Enterprise > db.colors.find({"type": "primary"})
{ "_id" : ObjectId("5a97de7f2fcdf731d255a19d"), "color" : "black", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 255, 255, 1 ], "hex" : "#000" } }
{ "_id" : ObjectId("5a97de7f2fcdf731d255a19e"), "color" : "yellow", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 255, 0, 1 ], "hex" : "#FF0" } }
{ "_id" : ObjectId("5a97de7f2fcdf731d255a1a1"), "color" : "red", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 0, 0, 1 ], "hex" : "#FF0" } }
{ "_id" : ObjectId("5a97de7f2fcdf731d255a1a2"), "color" : "blue", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 0, 0, 255, 1 ], "hex" : "#00F" } }
MongoDB Enterprise >

We have successfully imported that data, and we have fetched the colors with type as primary. I think, there should be a custom color, which has the type as primary. Can we do that now?

MongoDB Enterprise > db.colors.insert({
... "color": "custome",
...  "category": "hue",
...  "type": "primary",
...  "code": {
...  "rgba": [255,1,255,1],
...  "hex": "#FF1"
...  }
... }
... )
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise >

Now if you run our previous query, you can see an output as preceding.

MongoDB Enterprise > db.colors.find({"type": "primary"})
{ "_id" : ObjectId("5a97de7f2fcdf731d255a19d"), "color" : "black", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 255, 255, 1 ], "hex" : "#000" } }
{ "_id" : ObjectId("5a97de7f2fcdf731d255a19e"), "color" : "yellow", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 255, 0, 1 ], "hex" : "#FF0" } }
{ "_id" : ObjectId("5a97de7f2fcdf731d255a1a1"), "color" : "red", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 0, 0, 1 ], "hex" : "#FF0" } }
{ "_id" : ObjectId("5a97de7f2fcdf731d255a1a2"), "color" : "blue", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 0, 0, 255, 1 ], "hex" : "#00F" } }
{ "_id" : ObjectId("5a97e3202c19f0e958477e06"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" } }
MongoDB Enterprise >

Updating a document in MongoDB

Wow, we have the data now. And we performed, Create and Read operations on our DB. It is time for updating the document. Let’s go ahead and add a new property “manuallyCreated” to the color we have created. It is going to help us in finding this kind of entries easily.

MongoDB Enterprise > db.colors.update({
... "color":"custome"
... }
... ,{
... $set:{"manuallyCreated":"True"}}
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise >

Let’s find all the manually created colors now, and yeah we know there is going to be one record.

MongoDB Enterprise > db.colors.find({"manuallyCreated":"True"})
{ "_id" : ObjectId("5a97e3202c19f0e958477e06"), "color" : "custome", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, 1 ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
MongoDB Enterprise >

Let’s update the color name to “custom” instead of “custome”, sorry for the typo.

MongoDB Enterprise > db.colors.update({"_id" : ObjectId("5a97e3202c19f0e958477e06")},
... {$set:{"color":"custom"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise >

Ah, I made a mistake. I ran the following query by mistake.

MongoDB Enterprise > db.colors.update({ "color":"custom" } ,{ $set:{"code.rgba" : [ 255, 1, 255]}} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Do you know, what that query just did? It just updated the rgba of our custom color to three point array “[ 255, 1, 255]”. That’s not what I wanted. Now what we can do? We need to add one value to that set. Let’s do that now.

MongoDB Enterprise > db.colors.update({ "color":"custom" } ,{ $addToSet:{"code.rgba" : "1" }} )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.colors.find({"manuallyCreated":"True"})
{ "_id" : ObjectId("5a97e3202c19f0e958477e06"), "color" : "custom", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 1, 255, "1" ], "hex" : "#FF1" }, "manuallyCreated" : "True" }
MongoDB Enterprise >

Please be noted that, there is one more update, which will just update the entire document. In this case, if you are not providing the value for each attributes, it will be overwriting the same. Let’s see an example.

MongoDB Enterprise > db.colors.update({"color":"red"},{"color":"test"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.colors.find({})

Here we just give a command to update the color red to color test, as we have not provided other attributes as part of our query, after running the query, there will be only one attribute that is color.

MongoDB Enterprise > db.colors.find({})
{ "_id" : ObjectId("5a97f73f2fcdf731d255a1f1"), "color" : "black", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 255, 255, 1 ], "hex" : "#000" } }
{ "_id" : ObjectId("5a97f73f2fcdf731d255a1f2"), "color" : "test" }
{ "_id" : ObjectId("5a97f73f2fcdf731d255a1f3"), "color" : "white", "category" : "value", "code" : { "rgba" : [ 0, 0, 0, 1 ], "hex" : "#FFF" } }
{ "_id" : ObjectId("5a97f73f2fcdf731d255a1f4"), "color" : "blue", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 0, 0, 255, 1 ], "hex" : "#00F" } }
{ "_id" : ObjectId("5a97f73f2fcdf731d255a1f5"), "color" : "yellow", "category" : "hue", "type" : "primary", "code" : { "rgba" : [ 255, 255, 0, 1 ], "hex" : "#FF0" } }
{ "_id" : ObjectId("5a97f73f2fcdf731d255a1f6"), "color" : "green", "category" : "hue", "type" : "secondary", "code" : { "rgba" : [ 0, 255, 0, 1 ], "hex" : "#0F0" } }
MongoDB Enterprise > db.colors.find({"color":"test"})
{ "_id" : ObjectId("5a97f73f2fcdf731d255a1f2"), "color" : "test" }
MongoDB Enterprise >

Deleting a document in MongoDB shell

So, we added a custom color, and later we found that it is no more needed in our document as we found an exact match with a different color. Now we need to remove the one we have added.

MongoDB Enterprise > db.colors.remove({"color":"custom"})
WriteResult({ "nRemoved" : 1 })
MongoDB Enterprise >

Let’s go and check again whether it is actually removed or not.

MongoDB Enterprise > db.colors.find({"color":"custom"})
MongoDB Enterprise >

The find query returns no records.

Deleting an entire collection

We have performed CRUD operations on a collection, what if we need to remove the entire collection?

MongoDB Enterprise > db.colors.drop()
true
MongoDB Enterprise >

 

With that, we are done with this post. I will be posting the continuation part of this series very soon. Till then, bye.

Conclusion

Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

Kindest Regards
Sibeesh Venu

 

 

TagsMongo CLI CommandsMongo CommandsMongo ShellsMongoDB
Previous Article

Video: 21 Easy Tips for Healthy Life

Next Article

Continue With Learning Indexes in MongoDB

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0

SibeeshVenu

I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

Related articles More from author

  • MongoDB

    Learn MongoDB With Me

    February 22, 2018
    By SibeeshVenu
  • MongoDB

    Continue With Learning Indexes in MongoDB

    March 7, 2018
    By SibeeshVenu
  • Socket.io Output
    CodeProjectMongoDBNode JS

    Creating a Chat Application in Node JS with Express, MongoDB, Mongoose and Socket.io

    December 4, 2017
    By SibeeshVenu
  • MongoDB Data Insertion
    MongoDBNode JS

    Using MongoDB on Node JS Application Using Mongoose

    December 3, 2017
    By SibeeshVenu
0

My book

Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

YouTube

MICROSOFT MVP (2016-2022)

profile for Sibeesh Venu - Microsoft MVP

Recent Posts

  • React Native Android Release with Azure DevOps and Google Play Store
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
  • Easily move data from one COSMOS DB to another
  • .NET 8 New and Efficient Way to Check IP is in Given IP Range
  • Async Client IP safelist for Dot NET
  • Post Messages to Microsoft Teams Using Python
  • Get Azure Blob Storage Blob Metadata Using PowerShell
  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
  • Integrate Azure App Insights in 1 Minute to .Net6 Application

Tags

Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
  • .NET
  • Achievements
  • ADO.NET
  • Android
  • Angular
  • Arduino
  • Article Of The Day
  • ASP.NET
  • Asp.Net Core
  • Automobile
  • Awards
  • Azure
  • Azure CDN
  • azure devops
  • Blockchain
  • Blog
  • Browser
  • C-Sharp Corner
  • C#
  • Career Advice
  • Code Snippets
  • CodeProject
  • Cognitive Services
  • Cosmos DB
  • CSS
  • CSS3
  • Data Factory
  • Database
  • Docker
  • Drawings
  • Drill Down Chart
  • English
  • Excel Programming
  • Exporting
  • Facebook
  • Fun
  • Gadgets
  • GitHub
  • GoPro
  • High Map
  • HighChart
  • How to
  • HTML
  • HTML5
  • Ignite UI
  • IIS
  • Interview
  • IoT
  • JavaScript
  • JQuery
  • jQuery UI
  • JQWidgets
  • JQX Grid
  • Json
  • Knockout JS
  • Linux
  • Machine Learning
  • Malayalam
  • Malayalam Poems
  • MDX Query
  • Microsoft
  • Microsoft ADOMD
  • Microsoft MVP
  • Microsoft Office
  • Microsoft Technologies
  • Microsoft Windows
  • Microsoft Windows Server
  • Mobile
  • MongoDB
  • Monthly Winners
  • MVC
  • MVC Grid
  • MySQL
  • News
  • Node JS
  • npm
  • Number Conversions
  • October 2015
  • Office 365
  • Office Development
  • One Plus
  • Outlook
  • Page
  • PHP
  • Poems
  • PowerShell
  • Products
  • Q&A
  • Raspberry PI
  • React
  • SEO
  • SharePoint
  • Skype
  • Social Media
  • Software
  • Spire.Doc
  • Spire.PDF
  • Spire.XLS
  • SQL
  • SQL Server
  • SSAS
  • SSMS
  • Storage In HTML5
  • Stories
  • Third Party Software Apps
  • Tips
  • Tools
  • Translator Text
  • Uncategorized
  • Unit Testing
  • UWP
  • VB.Net
  • Videos
  • Virtual Machine
  • Visual Studio
  • Visual Studio 2017
  • Wamp Server
  • Web API
  • Web Platform Installer
  • Webinars
  • WebMatrix
  • Windows 10
  • Windows 7
  • Windows 8.1
  • Wordpress
  • Writing

ABOUT ME

I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

Contact Me

  • info@sibeeshpassion.com

Pages

  • About
  • Search
  • Privacy Policy
  • About
  • Search
  • Privacy Policy
© Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
Go to mobile version