Practical MongoDB shell commands every developer should know

The codingmarks are persisted in a MongoDB Server. Very often I find myself in the situation, where I need to modify or look for something in the mongo database. Experience has taught me that interacting with a system via shell commands helps me understand it better, and sort of brings me closer to it. Ok, so how to find the right mongo shell command? Well, I google for it of course, and most likely I am pointed to the right entry in the Mongo manual. In this post I try to consolidate the commands I usually use, so that I have only one codingmark to look for…

Octocat Source code for codingmarks.org is available on Github - frontend and backend.

Start the mongo shell

To start the mongo shell and connect to the MongoDB instance running on localhost with default port(which is 27017), change to the MongoDB installation directory:

> cd <mongodb installation dir>

and then type ./bin/mongo to start mongo

If you are like me, and hooked on aliases, you might use something like alias mongo-start-client='~/dev/mongodb/bin/mongo'. You can also add <mongodb installation dir>/bin to the PATH environment variable and then you can just type mongo

MongoDB does not enable access control by default. This might be fine for development, but for a production environment it is highly recommended to employ authorization. Please see the Create database and users section of the MongoDB Setup For Production wiki page for commands related to that.

Quit the mongo shell

You can type the following command to exit the mongo shell:

> quit()

Display database and collections

You can print a list of all available database on the server via show dbs command:

> show dbs
admin                  0.000GB
codingpedia-bookmarks  0.000GB
local                  0.000GB

To switch to the codingpedia-bookmarks database, use the use command:

> use codingpedia-bookmarks
switched to db codingpedia-bookmarks

Print the list of all collections of the current database:

> show collections
bookmarks

Find documents

Find all documents

To find all documents from collection, type the following:

> db.bookmarks.find()

Response

{ "_id" : ObjectId("57f3f0ba5fac7f3264354144"), "name" : "Test bookmark", "url" : "some titi url", "description" : "some description", "category" : "test", "tags" : [ "testing", "setup" ], "__v" : 0, "updatedAt" : ISODate("2016-10-10T08:18:32.563Z") }
{ "_id" : ObjectId("57f3f1235fac7f3264354146"), "name" : "Second bookmark", "url" : "some other url", "description" : "some description", "category" : "test", "tags" : [ "testing", "setup" ], "__v" : 0 }
{ "_id" : ObjectId("57f484ac1f991e33ab5e50bb"), "name" : "java bookmark", "url" : "java url", "description" : "some description", "category" : "java", "tags" : [ "testing", "setup" ], "__v" : 0 }
{ "_id" : ObjectId("57f5cd5975bf4e35310426f7"), "name" : "git bookmark", "url" : "git url", "description" : "some git description", "category" : "git", "tags" : [ "git command", "setup" ], "__v" : 0 }
{ "_id" : ObjectId("57f5cd7c453b2a353ba33f73"), "name" : "js bookmark", "url" : "js url", "description" : "some js description", "category" : "git", "tags" : [ "git command", "setup" ], "__v" : 0 }
{ "_id" : ObjectId("57f5db18eab35f36151691f4"), "name" : "wildfly bookmark", "url" : "wildfly url", "description" : "some js description", "category" : "git", "tags" : [ "git command", "setup" ], "__v" : 0 }
{ "_id" : ObjectId("57f5dbd43910e93642b25d6d"), "name" : "jboss bookmark", "url" : "jboss url", "description" : "some js description", "category" : "git", "tags" : [ "git command", "setup" ], "__v" : 0 }
{ "_id" : ObjectId("57f5dcf6517aa1366b0526fa"), "name" : "linux bookmark", "url" : "linux url", "description" : "some js description", "category" : "git", "tags" : [ "git command", "setup" ], "__v" : 0 }
{ "_id" : ObjectId("57f5dd573fed7e3677b20ca5"), "name" : "windows bookmark", "url" : "windows url", "description" : "some js description", "category" : "git", "tags" : [ "git command", "setup" ], "__v" : 0 }

Find documents with filter

Filter by one attribute (here filter by location):

> db.bookmarks.find({location: "http://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_cn/"});
{ "_id" : ObjectId("5948ab65ce8e01b7e330b330"), "name" : "Git magic", "location" : "http://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_cn/", "tags" : [ "free-programming-books-zh", "版本控制" ], "description" : "", "descriptionHtml" : "<p></p>", "createdAt" : ISODate("2017-06-20T04:58:13.192Z"), "shared" : true, "userId" : "2d6f6cb5-44f3-441d-a0c5-ec9afea98d39", "language" : "zh" }

Check out the find documention for more details.

Query an array

Match an array

Query for all documents where the field tags value is an array with exactly three elements, “angular”, “angular-cli” and “dev-tools”, in the specified order:

> db.bookmarks.find( { tags: ["angular", "angular-cli", "dev-tools"] } )

If, instead, we wish to find an array that contains all the elements “angular”, “angular-cli” and “dev-tools”, without regard to order or other elements in the array, we use the $all operator:

> db.bookmarks.find( { tags: { $all: ["angular", "angular-cli", "dev-tools"] } } )

Query an Array for an Element

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value.

The following example queries for all documents where tags is an array that contains the string “angular” as one of its elements:

> db.bookmarks.find({tags:"angular-cli"})

Sort results

You can apply sort() to the cursor before retrieving any documents from the database.

For example let’s sort documents by updatedAt Date (1 for ascending and -1 for descending): docs

> db.bookmarks.find().sort({updatedAt:1});
> db.bookmarks.find().sort({updatedAt:-1});

See doku for more details.

Delete/Remove documents

Remove all documents from collection:

> db.bookmarks.remove()

Removes all bookmarks for user, identified by userId

> db.bookmarks.remove({userId:"2d6f6cb5-44f3-441d-a0c5-ec9afea98d39"});
WriteResult({ "nRemoved" : 4 })

Remove with attributes (all but where shared is not set to true):

> db.bookmarks.remove({shared:{$ne:true}})
WriteResult({ "nRemoved" : 9 })

For more details see the db.collection.remove() documentation

Update documents

Update field value

Update githubURL for document with the given location (we know that is unique):

> db.bookmarks.update({ location : "http://www.codingpedia.org/" }, { githubURL : "https://github.com/Codingpedia/codingpedia.github.io"} );

Add new field

Add new language field and set it to en for all documents:

> db.bookmarks.update({}, {$set: {language: "en"}}, {multi: true});

For more update options, like upsert, $unset etc, please see the db.collection.update() manual.

Rename a field

To rename a field, call the $rename operator with the current name of the field and the new name:

> db.bookmarks.updateMany({}, { $rename: {"url":"location"} })

This operation renames the field url to location for all documents in the collection. To see how to rename embedded fields and more see the documentation for $rename

Indexing

Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.

Show indexes

Before creating an index we should see the existing indexes on a collection, by typing the following command:

> db.bookmarks.getIndexes()

[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "codingpedia-bookmarks.bookmarks"
        }
]

By default, all collections have an index on the _id field

Create index

MongoDB provides complete support for indexes on any field in a collection of documents. In addition to the index on the _idfield applications and users may add additional indexes to support important queries and operations.

Why you might want to create indexes - well, because indexes improve the efficiency of read operations by reducing the amount of data that query operations need to process. Please see the Query Optimization documentation entry for more details

Some exaples:

Create single index for the userId field

> db.bookmarks.createIndex( { userId: 1 } )

Value 1 means the index is sort ascending on the field

Create unique index for the location field:

> db.bookmarks.createIndex( { location: 1 }, { unique: true } );

Show the newly created indexes after:

> db.bookmarks.getIndexes()

[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "codingpedia-bookmarks.bookmarks"
        },
        {
                "v" : 1,
                "key" : {
                        "userId" : 1
                },
                "name" : "userId_1",
                "ns" : "codingpedia-bookmarks.bookmarks"
        },
        {
                "v" : 1,
                "unique" : true,
                "key" : {
                        "location" : 1
                },
                "name" : "location_1",
                "ns" : "codingpedia-bookmarks.bookmarks",
                "background" : true
        }
]

Verify if index is used for a query with the explain method:

> db.bookmarks.find({userId:"2d6f6cb5-44f3-441d-a0c5-ec9afea98d39"}).explain("queryPlanner")
{
	"queryPlanner" : {
		"plannerVersion" : 1,
		"namespace" : "codingpedia-bookmarks.bookmarks",
		"indexFilterSet" : false,
		"parsedQuery" : {
			"userId" : {
				"$eq" : "2d6f6cb5-44f3-441d-a0c5-ec9afea98d39"
			}
		},
		"winningPlan" : {
			"stage" : "FETCH",
			"inputStage" : {
				"stage" : "IXSCAN",
				"keyPattern" : {
					"userId" : 1
				},
				"indexName" : "userId_1",
				"isMultiKey" : false,
				"isUnique" : false,
				"isSparse" : false,
				"isPartial" : false,
				"indexVersion" : 1,
				"direction" : "forward",
				"indexBounds" : {
					"userId" : [
						"[\"2d6f6cb5-44f3-441d-a0c5-ec9afea98d39\", \"2d6f6cb5-44f3-441d-a0c5-ec9afea98d39\"]"
					]
				}
			}
		},
		"rejectedPlans" : [ ]
	},
	"serverInfo" : {
		"host" : "adrians-mbp.home",
		"port" : 27017,
		"version" : "3.2.9",
		"gitVersion" : "22ec9e93b40c85fc7cae7d56e7d6a02fd811088c"
	},
	"ok" : 1
}

Drop index

We use db.collection.dropIndex(index) to drop or remove an index from a collection. Let’s drop the unique “name” index from index list shown above. To drop the index ‘name’, we can either use the index name:

> db.bookmarks.dropIndex("name_1");

Or we can use the index specification document (key) { "name" : 1 } :

> db.pets.dropIndex( { "name" : 1 } );

The more I use MongoDB, probably the bigger this blog post will get.

Octocat Source code for codingmarks.org is available on Github - frontend and backend.

Adrian Matei

Adrian Matei
Life force expressing itself as a coding capable human being

Free Programming Books are now on Codingmarks.org

We're happy to announce that we've reached and surpassed our goal of 1 Mb #codingmarks, by importing the books from the Free-Programming-Books project Continue reading