Session Resources – NoSQL Explained – Demo 1: MongoDB Database create, data insert and data find

To conduct the first demo in the Instruct session on NoSQL you can follow along with the code below.
This MongoDB demo creates a database, inserts data and finds it.
All these commands are to be typed in sequentially into the command prompt. Please ensure that your MongoDB instance is running before starting.
If you are unsure how to get an instance of MongoDB running please view the Session Resources – NoSQL Explained – Running an MongoDB instance page for details on how to get an instance of MongoDB running on your machine.

We are going to run this demo from the MongoDB command prompt, run the following command.

c:\data_mongo\bin\mongo

This code will start an instance of the MongoDB command prompt which will connect to an instance of MongoDB server running on localhost and listening on the default port 27017.

To connect to an instance of MongoDB running on another server include the machine name and port as command parameters like this.

c:\data_mongo\bin\mongo mumbai:40001

Now for a few quick demo items, type the following commands in sequence to create a database, insert a record and find it.


//Set context to a new database called Demo1.
//Note the database hasn't been created yet we have just told MongoDB
//that we wish to use the database called Demo1

use Demo1

//Create a users collection and insert a document entity into the
//database.
//Note MongoDB will now create a database called Demo1 and include the
//users document collection.
//A new document will be created in the users collection with the
//following parameters:
//username, password, salt, isLockedOut, failedPasswordAttempt

db.users.insert({ username : "Chris.Tite",
password : "FslPhcKizgtFNt42HM+CpLSI3wk=",
salt : "V7BcIDv6T0M=", isLockedOut : false, failedPasswordAttempt : 0 })

//To check that the document was created run a find command on the
//users collection.
//Note that the find command will list all documents created in the
//users collection

db.users.find()

//inserts a second entity into the database
//Notice that the structure for this insert has an extra parameter
//called "pin".
//MongoDB has no problem inserting this slightly different document
//structure.

db.users.insert({ username : "Sally.Burnett",
 password : "R29evUC0dGwkLzCd5rT91b8n+xU=",
 pin : "a+ZRoz4kD+4jRx8MG8QcMQv2hIA=", salt : "VlMIEUalxZU==",
 isLockedOut : false, failedPasswordAttempt : 0 })

//View both new documents by doing another find

db.users.find()

//To find an entry in the users collection simply add the parameter
//you wish to search by.
//Note the json structure

db.users.find({ username : "Chris.Tite" })

//To find a single entry use the findOne command

db.users.findOne({ isLockedOut : false })

//To update a document use the update command.
//In the case below we are updating all entities in the users
//collection setting the isActive attribute to true
//Notice the empty braces {} as the first parameter in the update
//statement)
//The second parameter is the value of the new or existing parameter
//The third parameter (optional) tells Mongo if it needs to upsert
//i.e. create a new document when no documenst meet the criteria
//The forth parameter (optional) tells Mongo that it should update
//multiple documents

db.users.update({}, {$set : {isActive : true}}, false, true)

//Here is another example of an update statement this time using a
//save command.
//This command updates all entities in the users collection setting
//the isApproved attribute to true

db.users.save({}, {$set : {isApproved : true}}, false, true)

//If you are interested to know what the save command does simply run
//the command with out the brackets
//You will notice the command prompt shows the javascript for the save
//command, it uses an update
// or insert depending on whether or not it finds a record.
//It is always handy to remember that running a command without the
//brackets shows the actual javascript for the command.

db.users.save

//To remove a document use the remove command.
//The code below removes the entity in the users collection with the
//username attribute matching "Chris.Tite"

db.users.remove({username : "Chris.Tite"})

Congratulations you have successfully created a new database which contains a users collection.
You have also successfully inserted two users into the users collection, done a few finds, updated the user documents and removed a user document from the users collection.

Proceed to demo two to walk through working with Mongo indexes.

Advertisements

One thought on “Session Resources – NoSQL Explained – Demo 1: MongoDB Database create, data insert and data find

  1. Pingback: NoSQL Explained Resources | Kaleidocoding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s