Using MongoDB Working Notes

mongodb-working-notesIf you’re new to mongoDB and are looking to use it on one of your IOT projects then you might find my working notes useful.

Install –

The database files are stored in c:\data\db on Windows (default) and in the folder /var/lib/mongodb on Linux (Raspberry pi).



Executables:

You need access to the executables to run the database. They are located in:

  • Linux – usr/bin
  • Windows: c:\”program files”\mongodb\server\3.6\bin

Once installed you may need to start the server. On Linux it is installed as a service and so is already started.

To see if the database server is already started then on Linux and Windows use netstat -at

If you see localhost:27017 or 127.0.0.1:27017

then it is started. On Linux use the following to manually start, check status and stop the server.

  • service mongodb start
  • service mongodb status
  • service mongodb stop

The server uses the port 27017 by default.

It is also configured to only allow access from clients on the local machine. To connect to the database server from a remote machine then edit the mongodb.conf file.

The mongodb.conf File

This file controls various aspects of the server. It is located in /etc/mongo.conf on LInux and is not created on Windows.

On Windows if you need to use it then you need to create it and start the server as follows:

c:\mongod --config c:/mongo/mongodb.conf

Note: assumes using the mongo folder you can put it anywhere you want.

To allow access from a remote machine (e.g below uses 192.168.1.154) add the following on Windows:

net:
port: 27017
bindIp: 127.0.0.1,192.168.1.154

Note: to allow access from all remote hosts use 0.0.0.0 in place of the IP address 192.168.1.154.

Note: Windows use the YAML file format and the space is important.

On Linux standard text file format is used and the entry is simply

port: 27017
bindIp: 127.0.0.1, 192.168.1.154

Creating Databases,Collections etc

Before we look at creating collections etc it is important to understand the relationships between databases collections and documents.

A mongoDB database sever can support multiple databases.

A database is comprised of Collections which are effectively equivalent to Tables in a SQL database.

Collections consist of documents which are equivalent to rows in a SQL database.

A Documents is a set of key:value pairs.

The command line tool mogo.exe(windows) or mongo (Linux,Pi) is used for managing the mongo database.

On the Linux command line type:

mongo

On Windows you need to go to the directory holding the file or add the directory to the path. The folder is:

c:\”program files”\mongodb\server\3.6\bin

On the Windows command line type:

mongo or mongo.exe

start-mongo-admin

Useful Commands

List existing databases (note: empty ones will not be shown) use show dbs:

mongo-show-databases
Create a database or switch to an existing database use:- use databasename

example use the test database

mongo-create-use-database

Delete a database then first switch to the database, and then use –db.drop.Database()

mongo-delete-database

To create a collection in a database then switch to the database and use: db.createCollection(name,options)

mongo-create-collection

You can also create a collection by inserting a document in it use- db.test2.insert({“key”:”value”})

mongo-insert-document

To see a list of all collections in database use show collections and to delete a collection use db.collection_name.drop()

mongo-delete-collections

To add a document use db.collection_name.insert(document)

a document consists of a set o key value pairs. e.g

nongo-insert-document

To get a list of all documents in a collection use- db.collection_name.find()

mongodb-find-documents

To remove a document use-db.collection_name.remove(Deletion Criteria)

Note: if you don’t specify any deletion criteria then all documents are removed.

To remove test entry 2 use:

db.testcollection.remove({title:”test entry 2″})

mongodb-remove-document

Creating Users

If you access the database from another machine then you will need to add a user.

Use the db.createUser() command

Example: User steve password of password with read and write access to database test2.

db.createUser({user:"steve",pwd:"password",roles:[{role:"readWrite",db:"test2"}]})

Resources :

Related Tutorials

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *