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

Learn MongoDB With Me (Part 2)

DZone's Guide to

Learn MongoDB With Me (Part 2)

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

· Database Zone ·
Free Resource

Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%! 

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 read it here. This is a 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 step I am following. I hope this will help you follow along with me. 

Image title

Background

Like I said, this is the second part of the series. I believe that you have enough knowledge about Mongo DB and how to set it up. How can Mongo Shells be used? If you are not able to answer these questions 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 whether the data is in .tsv, .csv, .json, etc. Let's see those in action.

As a first step, let's see some documentation.

C:\Program Files\MongoDB\Server\3.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 Into MongoDB Using Mongo Shell

Now let's say I have the following JSON data. We are going to insert it into our database 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 database name, collection, name, data type, and file name.

If you ever get the error 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 the following output.

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 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 with 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 will see the following output:

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 database. 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 these kinds 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 the 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 note 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 attribute, 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 the 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 was no longer needed in our document as we found an exact match with a different color. Now, we need to remove the one we 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. 

Running out of memory? Never run out of memory with Redis Enterprise databaseStart your free trial today.

Topics:
database ,mongodb ,tutorial ,mongo shell ,cli

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}