Wednesday 12 February 2014

First Steps in MongoDB

First Steps in mongoDB

In this article, I'm going to use the mongo shell to play with some simple DB queries. See the post on installation if you don't yet have mongo installed. Here is what I'll be doing today
  • Starting a new collection to store some simple documents
  • Putting some documents into the collection
  • Listing the contents of the collection
  • Searching the collection for documents with certain values (e.g Male)
  • Searching the collection for documents with certain keys (e.g.Gender)
  • Removing a document from the collection
Later posts will cover more complex searching using regular expressions and tasks such as listing all keys in a collection.

Get a Collection

A collection is somewhere to put a set of related documents. The relationship is loose - not like a relational database, just things that belong together. You can create one like this:

db.creatCollection(colname)

where colname is the name you want to give your new collection, and you refer to existing collections as:

db.colname

For example,

db.createCollection("books")

and you refer to the collection as

db.books

Note the " quotes around the creation (it is a string here) but not around the reference, where it is a database object. Also, you do not have to create the collection before using it.

Not sure what collections your database contains? No problem:

show collections

will tell you.

You can just insert data into a collection that doesn't exist, and one will be created. How to do that is next:

Insert a Document

No surprises here, you insert a document using

db.books.insert(doc)

where doc is defined as a set of key: value pairs like this:

doc={ k1:v1, k2:v2 ... }

for example,

db.books.insert({Name:"On the Road", Author: "Jack Kerouac"})

Or, if we first say

b= db.books

then we can just use

b.insert (.....)

as a shorter way to reference the collection.

Perhaps the next book doesn't have an author, but an editor, so:

db.books.insert({Name:"Collected Poems", Editor: "J.E Bowles"})

Notice, we haven't defined a table structure - we just add documents in this style and new keys are added as needed.

List the Contents of a Collection

The find() command is used to retrieve data from a MongoDB:

b.find()

returns every entry in the collection. We will learn to iterate over rows later, for now just see that this produces:

> db.books.find()
{ "_id" : ObjectId("52658e0a84b47fef69ebab5f"), "Name" : "On the Road", "Author" : "Jack Kerouac" }
{ "_id" : ObjectId("52658e4984b47fef69ebab60"), "Name" : "Collected Poems", "Editor" : "J.E Bowles" }

Note the _id given to each document in the collection - MongoDB assigns a unique ID to each one as they are entered.

Search for Certain Values

Searches are defined in much the same way as data is entered, by specifying key: value pairs:

b.find({Name : "Collected Poems"})

Searches for the book with the Name of "Collected Poems". We will look at more complex searches in another blog.

Search for Certain Key

Perhaps you want to know all the books with an editor, but don't know any of the editors' names. You can search for documents with a certain key like this:

db.books.find( { Editor:{ "$exists":"true"}})

This is our first peek at the $, which prefixes a set of MongoDB operators, described here. We will look at them in a future blog.

Removing a Document

To remove a document from a collection, use

db.books.remove({Editor:"J.E. Bowles"})

Note that the syntax is the same as for find (except the word remove replaces the word find, of course).

Well, that's it for now. Next stop, more detailed searching.

No comments:

Post a Comment