MongoDB search in C#
        Posted  
        
            by 
                user3684208
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user3684208
        
        
        
        Published on 2014-06-08T15:20:53Z
        Indexed on 
            2014/06/08
            15:24 UTC
        
        
        Read the original article
        Hit count: 217
        
I have a problem with querying MongoDB. In my code I have a method Get which has as a parametar a Dictionary. It should go through the database and query it, comparing string and then object. So, i always get a problem with this object part, QueryDocument won't take in an object type because it isn't an BsonValue. I have tried to cast it but it won't work. Do you have any suggestions ? Thanks
Code part :
public List<ExceptionViewModel> Get(Dictionary<string, object> FilteredExceptions)
    {
        MongoClient mongo = new MongoClient();
        MongoServer mongoServer = mongo.GetServer();
        MongoDatabase db = mongoServer.GetDatabase("Aplikacija");
        MongoCollection collection = db.GetCollection("Exceptions");
        List<ExceptionViewModel> Get = new List<ExceptionViewModel>();
        foreach (KeyValuePair<string,object> item in FilteredExceptions)
        {
            var query = new QueryDocument(item.Key.ToString(),item.Value);
            foreach (ExceptionViewModel exception in collection.FindAs<ExceptionViewModel>(query))
            {
                Console.WriteLine("{0}", exception.BrowserName);
            }
        }
        return Get;
    }
© Stack Overflow or respective owner