Query Untyped Object Inside a MongoDB

jieforest發表於2012-05-28

I’ve blogged about using MongoDb to store log4net logs,


but the original appender has a littleproblem, it stores a BsonDocument inside the MongoCollection, and does not use any C# object andwhen it is time to query data you cannot use the new LINQ Mongo provider that is included in newest C# drivers (1.4), because data is completely untyped.


This is usually not a big problem, because you can querya collection using simple JSON-like query, but if you are used to standard LINQ provider, probably you will get alittle bit lost on how to create the JSON query to retrieve the data you need.


This is the scenario I need to solve:


I have a stupid Winform. that is able to show someinformation from a standard Log4Net Sql database (using the adonetappender) andI want to be able to use the very same interface to load data from mongodatabase.


First of all I need to load all distinct value for the property level and loggerName, because I have acombo where the user can filter for Severity (ERROR, WARN, INFO, Etc.) and alist of checkboxes used to filter for loggerName. Luckily enough, mongo offersuch a functionality out of the box.

CODE:

var db = server.GetDatabase(bsDatabase.Current as string);
var collection = db.GetCollection(bsCollections.CurrencyManager.Current as string);
var allLevel = collection.Distinct("level");

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-731253/,如需轉載,請註明出處,否則將追究法律責任。

相關文章