Create and query a MongoDb database from the command line

in nosql •  7 years ago 

NoSQL databases are the go-to storage tool for prototyping applications that do not stringently operate with normalized, strictly-structured data. Using a NoSQL database allows developers to prototype their idea without having to define their schema upfront, thus enabling them to add and remove fields on the fly as the application evolves. Add to that the fact that NoSQL databases generally scale quite well, it is not a surprise that a lot of developers turn to a NoSQL database when putting together a stack for their application prototype.

MongoDb

MongoDb is often a NoSQL database developers turn to for such applications. It is a mature product which offers a rich query language. The following tutorial will outline steps to take in order to create and query a MongoDb database from the command line.

Prerequisites

To follow along you will need:

  • MongoDb 3.0.0 > installed on your server.
  • Command line access to your server with MongoDb server started.

Step 1 - Create an example MongoDb database.

Interacting with your MongoDb instance is made easy using the mongo shell. Access it with the mongo command:

$ mongo

The mongo shell will load and you will see output that looks something like the following:

MongoDB shell version: 2.4.9
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
>

To create an example database, simpy use the use command as follows (our example database is called dbExample:

> use dbExample

You will see output that looks like the following:

switched to db dbExample

This indicates that the dbExample database has been created.

Step 2 - Insert data into a collection

Data within a MongoDb database belongs to collections. However, collections do not have to be created explicitly. Instead, an insert command when written to a non-existent collection will implicitly create that collection and insert data as a document into it after the fact.

> db.myExampleCollection.insert([{'name':'John Doe','age':35}]);

Step 3 - Query inserted documents from your collection:

Now that we have inserted a document into the myExampleCollection colletion, we can query it with the following command:

> db.myExampleCollection.find();

The query will return the following result:

{ "_id" : ObjectId("5a4da635b5195299b00ae589"), "name" : "John Doe", "age" : 35}

Step 4 - Delete data from a collection:

Documents can be deleted from a collection using the remove command.

db.myExampleCollection.deleteOne( {'name': 'John Doe' } );

After you execute the above command you will find that a subsequent find command executed against the myExampleCollection collection will yield no results.

I hope you have enjoyed this tutorial. Don't forget to vote up, follow and share!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...

Congratulations @dnx! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!