MongoDB

MongoDB is powered by JavaScript and Mongo shell acts as a JavaScript environment. So, you can execute standard Mongo queries, as well as standard JavaScript statements.

Mongo shell

Mongo shell is accessed via terminal using the command mongo.

ddn_ro@ddn-ro-linux:~$ cd Documents/ProjectOne
ddn_ro@ddn-ro-linux:~/Documents/ProjectOne$ mongo
MongoDB shell version: 2.6.10
connecting to: test
>

By default, you are connected to the local server and set to use the test database. You can display a list of all databases using the command show dbs.

ddn_ro@ddn-ro-linux:~/Documents/ProjectOne$ mongo
MongoDB shell version: 2.6.10
connecting to: test
> show dbs
admin       (empty)
local       0.078GB
myproject   0.078GB
projectone  0.078GB
> 

You can switch to any of the listed databases by using the command use followed by the name of the database.

> use projectone
switched to db projectone
> 

Note:
If you ask for a database and it does not exist then MongoDB will create it for you automatically.

To view a list of collections use the command show collections.

> show collections
articles
system.indexes
> 

Note:
Collections in MongoDB are similar to the tables in a relational database.

To insert data in a database use the command db.newCollection.insert({ name: 'Dan', website: 'http://dan.saigon.ro' }) where you can replace with your own information everything is inside parenthesis.

To view the database use the command db.newCollection.find() .pretty().

> db.newCollection.insert({ name: 'Dan', website: 'http://dan.saigon.ro' })
WriteResult({ "nInserted" : 1 })
> db.newCollection.find() .pretty()
{
	"_id" : ObjectId("5946fe223c1592c5d7065823"),
	"name" : "Dan",
	"website" : "http://dan.saigon.ro"
}
>

Note:
We practically inserted a JSON object.

On the find() command we get in return three fields. One of them is an automatic addition named _id. It is Mongo’s method for tracking a unique identifier for every record. The pretty() is used to have the output presented prettier.

I have inserted two more records for testing purposes.

> db.newCollection.insert({ name: 'Tra', website: 'http://tra.saigon.ro' })
WriteResult({ "nInserted" : 1 })
> db.newCollection.insert({ name: 'Sonia', website: 'http://sonia.saigon.ro' })
WriteResult({ "nInserted" : 1 })
> db.newCollection.find() .pretty()
{
	"_id" : ObjectId("5946fe223c1592c5d7065823"),
	"name" : "Dan",
	"website" : "http://dan.saigon.ro"
}
{
	"_id" : ObjectId("594700943c1592c5d7065824"),
	"name" : "Tra",
	"website" : "http://tra.saigon.ro"
}
{
	"_id" : ObjectId("594700b33c1592c5d7065825"),
	"name" : "Sonia",
	"website" : "http://sonia.saigon.ro"
}
> 

Searching for records in your collection is done with the command find(). You can also provide a parameter to narrow the search.

> db.newCollection.find({ name: 'Sonia' })
{ "_id" : ObjectId("594700b33c1592c5d7065825"), "name" : "Sonia", "website" : "http://sonia.saigon.ro" }
> 

You can also include additional parameters.

> db.newCollection.find({ name: 'Sonia', website:'http://sonia.saigon.ro' })
{ "_id" : ObjectId("594700b33c1592c5d7065825"), "name" : "Sonia", "website" : "http://sonia.saigon.ro" }
> 

If you want only a specific field to be included you can add a second parameter to find().

> db.newCollection.find({ name: 'Sonia' }, { name: true })
{ "_id" : ObjectId("594700b33c1592c5d7065825"), "name" : "Sonia" }
>

Note:
The _id field is always included (you can specify not to be).

You can also search for things that are within ranges!

To update a record in the MongoDB use the update() function and include the first query as the first parameter.

> db.newCollection.update({ name: 'Sonia' }, { $set: { age: '9' } })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.newCollection.find({ name: 'Sonia' })
{ "_id" : ObjectId("594700b33c1592c5d7065825"), "name" : "Sonia", "website" : "http://sonia.saigon.ro", "age" : "9" }
>

Now, the record contains one more field, age.

Deleting records in the MongoDB is done with remove() function.

>db.newCollection.remove({ name: 'Sonia' })

If you want to delete every document from a collection then use the function drop().

>db.newCollection.drop()

Leave a Reply