Learn MongoDB With Me – Part 2
[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\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 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