Posts Tagged ‘MongoDB

Install MongoDB as a Windows Service

Installing MongoDB as a service on my Windows 7 turned out to be pretty simple. 

Download Mongo – http://www.mongodb.org/downloads

Follow Instructions – http://www.mongodb.org/display/DOCS/Windows+Service

There was one gotcha I ran into as I did the install.  If you specify an invalid data path the service will be stuck in an infinite loop of restarts, and have to be removed (or create the missing directory).

My install command :

   1: D:\Tools\MongoDB\mongodb-win32-x86_64-2.0.2\bin\mongod --logpath C:\Data\Development\logs\MongoDB.log --logappend --dbpath C:\Data\Development\MongoDB_Data --install --serviceName MongoDB

 

Here is the exception I was getting in my log file:

   1: ***** SERVER RESTARTED *****

   2:  

   3:  

   4: Sun Mar 04 10:20:08 [initandlisten] MongoDB starting : pid=6652 port=27017 dbpath=C:\Data\Development\OneBrownMule\OneBrownMule.Web\App_Data\AppDB 64-bit host=mueller-laptop2

   5: Sun Mar 04 10:20:08 [initandlisten] db version v2.0.2, pdfile version 4.5

   6: Sun Mar 04 10:20:08 [initandlisten] git version: 514b122d308928517f5841888ceaa4246a7f18e3

   7: Sun Mar 04 10:20:08 [initandlisten] build info: windows (6, 1, 7601, 2, 'Service Pack 1') BOOST_LIB_VERSION=1_42

   8: Sun Mar 04 10:20:08 [initandlisten] options: { dbpath: "C:\Data\Development\MongoDB_Data", logappend: true, logpath: "C:\Data\Development\logs\MongoDB.log", service: true }

   9: Sun Mar 04 10:20:08 [initandlisten] exception in initAndListen: 10296 dbpath (C:\Data\Development\MongoDB_Data) does not exist, terminating

  10: Sun Mar 04 10:20:08 dbexit: 

  11: Sun Mar 04 10:20:08 [initandlisten] shutdown: going to close listening sockets...

  12: Sun Mar 04 10:20:08 [initandlisten] shutdown: going to flush diaglog...

  13: Sun Mar 04 10:20:08 [initandlisten] shutdown: going to close sockets...

  14: Sun Mar 04 10:20:08 [initandlisten] shutdown: waiting for fs preallocator...

  15: Sun Mar 04 10:20:08 [initandlisten] shutdown: lock for final commit...

  16: Sun Mar 04 10:20:08 [initandlisten] shutdown: final commit...

  17: Sun Mar 04 10:20:08 [initandlisten] shutdown: closing all files...

  18: Sun Mar 04 10:20:08 [initandlisten] closeAllFiles() finished

  19: Sun Mar 04 10:20:08 dbexit: really exiting now

Tags :

Remove by Id–MongoDB C#

When removing a document by the identifier using the C# 10gen driver for MongDB you need to specify the internally used id column, “_id”, not the property name from the DTO that you’re storing.

The Driver can be found on NuGet by search for MongoDB or the official project github page can be found here.

   1: public class DataProvider 

   2: { 

   3:     private MongoDatabase _db;

   4:     public DataProvider() 

   5:     { 

   6:         var server = MongoServer.Create("mongodb://127.0.0.1"); 

   7:         _db = server.GetDatabase("MyDB"); 

   8:     }

   9:     public void Delete(string id) 

  10:     { 

  11:         var collection = _db.GetCollection("MyDocumentCollection");

  12:         //Does not work 

  13:         //collection.Remove(new QueryDocument("Id", new BsonObjectId(id)));

  14:         //Works 

  15:         collection.Remove(new QueryDocument("_id", new BsonObjectId(id))); 

  16:     } 

  17: }

  18: public class MyDocument 

  19: { 

  20:     [BsonId] 

  21:     public ObjectId Id { get; set; } 

  22:     public string Name { get; set; } 

  23:     public string Description { get; set; } 

  24: }

 

I’ve submitted a pull request to have RemoveById added to the MongoCollection.

https://github.com/mongodb/mongo-csharp-driver/pull/87

Tags : ,