Results 1 to 7 of 7

Thread: MongoDB

  1. #1

    Default MongoDB


    Anyone familiar with MongoDB? and its drivers/api, specifically the C# driver?

  2. #2
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1

    Default Re: MongoDB

    it works well in python...ewan ko lng sa C# ...just curious, what project ur working at?

  3. #3

    Default Re: MongoDB

    I'm creating a SessionStateStore provider, which uses MongoDB as the data storage. I'm using the C#.NET driver version 1.0. This will be use to support Session Distribution across Web Farms.

    The thing is it works fine storing .NET native types, however there is an issue storing class derived objects. Ahmm the issue is not actually storing, since I can store it, the issue I'm facing is when you try to update a record which the value is not of .NET native type.

  4. #4

    Default Re: MongoDB

    Quote Originally Posted by fixyourself View Post
    I'm creating a SessionStateStore provider, which uses MongoDB as the data storage. I'm using the C#.NET driver version 1.0. This will be use to support Session Distribution across Web Farms.

    The thing is it works fine storing .NET native types, however there is an issue storing class derived objects. Ahmm the issue is not actually storing, since I can store it, the issue I'm facing is when you try to update a record which the value is not of .NET native type.
    I haven't used MongoDB but reading their documentation, it's some Javascript-based database. Interesting stuff. How are you storing the objects? I presume you are serializing these objects into JSON and save it? Or does the C# driver *supposedly* handles these automatically?

    SQL to Mongo Mapping Chart - MongoDB

  5. #5

    Default Re: MongoDB

    Quote Originally Posted by SYNCH View Post
    I haven't used MongoDB but reading their documentation, it's some Javascript-based database. Interesting stuff. How are you storing the objects? I presume you are serializing these objects into JSON and save it? Or does the C# driver *supposedly* handles these automatically?

    SQL to Mongo Mapping Chart - MongoDB
    the C# driver does it, you just have to supply values to the Bson Type Document.

  6. #6

    Default Re: MongoDB

    Quote Originally Posted by fixyourself View Post
    the C# driver does it, you just have to supply values to the Bson Type Document.
    I was playing with this Try MongoDB

    I was wondering how are you storing the data in the first place? I presume, it's gonna be like:
    Code:
    // object
    internal class Person 
    {
       internal string Name { get; set; };
       internal Address Address { get; set; };
    }
    
    internal class Address
    {
       string St_No; string St_Name;
    }
    
    // some instance
    var user = new Person { Name="John", Address=new Address { St_No="5", St_Name="Cebu" } };
    
    
    
    // C# driver call here, translation to BSON ?? I presume you can call stuff
    // like below?? I'm assuming that the "Address" is what I understood the problem??
    
    
    
    // data
    db.users.save({ name: 'John', address: { st_no: '5', st_name: 'Cebu' } });
    db.users.find();
    
    // I just tried doing something like, and it ?worked:
    db.users.update({ name: 'John' }, { address: { st_no: '1', st_name: 'Naga' } });
    EDIT:
    Sorry, I just looked at the C# tutorial for MongoDB, CSharp Driver Tutorial - MongoDB. I haven't thought it's gonna be a wrapper-type library, I was expecting closer like the above. This could be an issue in the C# library itself alone, worst case dive to the code.

  7. #7

    Default Re: MongoDB

    This is how the Bson Mapped class looked like
    Code:
        internal class SessionElement
        {
            [BsonId(IdGenerator = typeof(CombGuidGenerator))]
            [BsonElement("_id")]        
            public Guid DocumentId { get; private set; }
    
            [BsonElement("ApplicationName")]
            public string ApplicationName { get; set; }
    
            [BsonElement("SessionOwner")]
            public string SessionOwner { get; set; }
    
            [BsonElement("Key")]
            public string Key { get; set; }
    
            [BsonElement("Value")]
            public object Value { get; set; }
    
            public SessionElement()
            {
                //DocumentId = Guid.NewGuid().ToString();
            }
    
    
            private static bool IsClassMapRegistered { get; set; }
            
            public static void RegisterClassMap()
            {
                if (!IsClassMapRegistered)
                {
                    BsonClassMap.RegisterClassMap<SessionElement>();
                    IsClassMapRegistered = true;
                }
            }
        }
    Actually we customized a SessionStateCollection to be used as the SessionStateCollection for the SessionStateStore. There are still some methods which are not implemented.

    Code:
    internal class StateItemCollection : ISessionStateItemCollection, ICollection, IEnumerable
        {
            private string m_connectionString = null;
            private string m_hostName = string.Empty;
            private string m_catalogName = string.Empty;
            private bool m_isDirty = false;
    
            public string SessionOwner { get; set; }
    
            public string ApplicationName { get; private set; }
    
            private MongoCollection<BsonDocument> CurrentCollection 
            {
                get
                {
                    MongoServer server = MongoDBUtil.NewConnection(m_hostName);
                    MongoDatabase database = MongoDBUtil.GetSessionStoreDatabase(server, m_catalogName);
                    return MongoDBUtil.GetSessionStoreCollection(database, MongoDBUtil.USER_SESSION_ITEM_COLLECTION);
                }
            }
    
            #region ConnectionString
            //format: "server=mongodb://localhost;catalog=SessionStorage"
            public string ConnectionString 
            {
                get
                {
                    return m_connectionString;    
                }
                set
                {
    
                    m_connectionString = value;    
                    ParseConnectionString(m_connectionString);
                }
            }
    
            private void ParseConnectionString(string connectionString)
            {
                string[] splits = connectionString.Split(';');
    
                if (splits.Count() > 2)
                    throw new Exception("connection string not correctly formed");
                
                m_hostName = GetConnectionValue("server", splits[0]);
                if (m_hostName == string.Empty)
                {
                    m_hostName = GetConnectionValue("server", splits[1]);
                    m_catalogName = GetConnectionValue("catalog", splits[0]);
                    return;
                }
                m_catalogName = GetConnectionValue("catalog", splits[1]);            
            }
    
            private string GetConnectionValue(string name, string uncleanstring)
            {
                uncleanstring = uncleanstring.Replace("=", string.Empty);
                if (uncleanstring.Contains(name))
                {                
                    return uncleanstring.Replace(name, string.Empty).Trim();
                }
                else if (uncleanstring.Contains(name))
                {
                    return uncleanstring.Replace(name, string.Empty).Trim();
                }
                return string.Empty;
            }
            #endregion
    
    
            #region MongoDB
            
            #endregion
    
       
    
            public StateItemCollection(string applicationName, 
                string connectionString)
            {            
                ApplicationName = applicationName;
                SessionOwner = Guid.NewGuid().ToString();
                ConnectionString = connectionString;            
            }
    
    
    
            #region ISessionStateItemCollection
    
            private object GetValue(string key)
            {
                IMongoQuery query = Query.And(Query.EQ("SessionOwner", SessionOwner), Query.EQ("ApplicationName", ApplicationName), Query.EQ("Key", key));           
                SessionElement doc = CurrentCollection.FindOneAs<SessionElement>(query);
                if (doc == null)
                    return null;
                return doc.Value;
            }
    
            private static Dictionary<Type, bool> m_knownTypes = null;
            private static Dictionary<Type, bool> KnownTypes
            {
                get
                {
                    if (m_knownTypes == null)
                        m_knownTypes = new Dictionary<Type, bool>();
                    return m_knownTypes;
                }
            }
    
            private void SetValue(string key, object value)
            {            
                //Check Classmap
                //if (!KnownTypes.ContainsKey(value.GetType()) && value.GetType().IsClass)
                //{
                //    BsonClassMap map = BsonClassMap.LookupClassMap(value.GetType());
                //    BsonClassMap.RegisterClassMap(map);
                //    KnownTypes.Add(value.GetType(), true);
                //}
                
    
                IMongoQuery query = Query.And(Query.EQ("SessionOwner", SessionOwner), Query.EQ("ApplicationName", ApplicationName), Query.EQ("Key", key));
    
                int count = CurrentCollection.Count(query);
                if (count == 1)
                {                
                    SessionElement document = CurrentCollection.FindOneAs<SessionElement>(query);
                    document.Value = value;
                    CurrentCollection.Save<SessionElement>(document);
                }
                else if (count == 0)
                { 
                    SessionElement document = new SessionElement();
                    document.SessionOwner = SessionOwner;
                    document.ApplicationName = ApplicationName;
                    document.Key = key;
                    document.Value = value;
                    CurrentCollection.Insert<SessionElement>(document);
                }           
              
            }
    
          
            public object this[string key]
            {
                get
                {
                    return GetValue(key);
                }
                set
                {
                    SetValue(key, value);
                }
            }
    
            public object this[int index]
            {
                get
                {
                    throw new NotImplementedException();
                }
                set
                {
                    throw new NotImplementedException();
                }
            }
    
            void ISessionStateItemCollection.Clear()
            {
                IMongoQuery query = Query.And(Query.EQ("SessionOwner", SessionOwner), Query.EQ("ApplicationName", ApplicationName));
                CurrentCollection.Remove(query);
            }
    
            void ISessionStateItemCollection.Remove(string key)
            {            
                IMongoQuery query = Query.And(Query.EQ("SessionOwner", SessionOwner), Query.EQ("ApplicationName", ApplicationName), Query.EQ("Key", key));
                CurrentCollection.Remove(query);
            }
    
            void ISessionStateItemCollection.RemoveAt(int index)
            {
                throw new NotImplementedException();
            }
    
            bool ISessionStateItemCollection.Dirty
            {
                get
                {
                    return m_isDirty;
                }
                set
                {
                    m_isDirty = value;
                }
            }
    
            System.Collections.Specialized.NameObjectCollectionBase.KeysCollection ISessionStateItemCollection.Keys
            {
                get
                {                
                    throw new NotImplementedException();  
    
                }
            }
    
            #endregion
    
            #region ICollection
    
            int ICollection.Count
            {
                get
                {
                    IMongoQuery query = Query.And(Query.EQ("SessionOwner", SessionOwner), Query.EQ("ApplicationName", ApplicationName));
                    return CurrentCollection.Count(query);
                }
            }
    
            bool ICollection.IsSynchronized
            {
                get
                {
                    return false;
                }
            }
    
            object ICollection.SyncRoot
            {
                get
                {
                    throw new NotImplementedException();
                }
            }
    
            
            void ICollection.CopyTo(System.Array array, int length)
            {
    
            }
    
            #endregion
    
            #region IEnumerable
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                IMongoQuery query = Query.And(Query.EQ("SessionOwner", SessionOwner), Query.EQ("ApplicationName", ApplicationName));
                var cursor = CurrentCollection.FindAs<SessionElement>(query);
                return cursor.GetEnumerator();
            }
    
            #endregion
    
        }

  8.    Advertisement

Similar Threads

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top