Mongodb and datalogging

Download the binary  from, for example  Open the zip file, copy the inside directory to C:\ and rename the directory as mongodb2.  Create a data directory default is “C:\data\db”.  Put a copy of the command prompt in c:\mongodb2 and set the startup properities to C:\mongodb2.  Open the command prompt, and cd C:\mongodb2\bin.  Execute c:\mongodb2\bin\mongod.  This should start the database server.  You need to keep this window open (minimized) unless you are running the database as a service.  You can also execute c:\mongodb2\bin\mongo to open the admin console.  You are done.

Not really, but you have an instance of mongodb running.

You also want to get drivers for the language of your choice.  There are serveral C#/powershell drivers, but there is one from 10Gen, the company, so why not?  Download the driver from github.  You have a choice of zip or MSI.  I choice the MSI, downloaded and ran it.  It installed to c:\program files\mongodb\CSharpDriver1.3.1.

NOW you are done.  Except for writing powershell code to log data.  This is the fun part – to come up with a schema that will let you get back your data in a useful way.  Since we are datalogging, it would seem that in addition to the autogenerated key, a date-time stamp would help as well as the data.  Sometimes we are just looking at BB properties, and sometimes at the different servers.  That SHOULD make a difference to us as we think about what we are going to what to get back out.

What seems to work is this:

Add-Type -Path "C:\Program Files\MongoDB\CSharpDriver 1.3.1\MongoDB.Driver.dll";
[MongoDB.Bson.BsonDocument] $BSONdoc = @{
 "_id"= [MongoDB.Bson.ObjectId]::GenerateNewId();
 "fieldone"= "data1";
 "fieldtwo"= "data2";
 "compositefield"= [MongoDB.Bson.BsonDocument] @{
 'cf1'= 'integer';
 'cf2'= 'text';
$db = [MongoDB.Driver.MongoDatabase]::Create('mongodb://localhost/BBlog');
$collection = $db['sessions'];
NB:  in some cases, this might NOT be best practice.  I was getting an error in mongo shell trying to show collection: (exception: db already exists with different case other:).  apparently there are problem with the CASE of the database?  Standardize with all l/c.

Running this outputs to the mongodb window the following:

Mon Feb 27 09:46:35 [initandlisten] connection accepted from #1
Mon Feb 27 09:46:35 [conn1] build index BBlog.sessions { _id: 1 }
Mon Feb 27 09:46:35 [conn1] build index done 0 records 0.001 secs
Mon Feb 27 09:47:07 [clientcursormon] mem (MB) res:32 virt:109 mapped:32

and all is good.

I admit i get nervous with the ::Create statement as formatted above.  You can also MongoServer.Create(“mongodb://localhost:27017”); followed by $db.GetDatabase(“bblog”).

It should be obvious (but not to me…) that that localhost could be replaced by something else, like


to connect to a server running on a different machine (the FQDN is not needed, but the port is).  So all the goodness of the javascript shell is now available to you from the warm fuzzes of your desktop.  And be nice and use exit when you are through.

another little note – if you get a message like “REALLY shutting down” and mongod won’t start, it probably means an unclean shutdown.  try to remove the mongod.lock in the database directory and restarting.  there is also a repair switch. 

Make it into a Windows service (before starting mongod) with this from the command line

mongod --logpath c:\mongo2db\logs\mongologs.log --logappend --dbpath c:\data\db --install

After you install, you still have to manually start the service, which is enlightenly called MongoDB with properties like “C:\mongodb2\bin\mongod” –logpath  “C:\mongodb2\logs\mongolog.log”  –service

This entry was posted in Uncategorized. Bookmark the permalink.