Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A .NET Document Mapper for MongoDB over MongoDB C# Driver

branch: master
README.md

MongoMapper.NET

A .NET Document Mapper for MongoDB over MongoDB C# Driver

Dependencies

Defining the Model

    [Serializable]
    [MongoKey(KeyFields = "Code")]
    public class Country: MongoMapper
    {        
        [MongoDownRelation(ObjectName = "Person", FieldName = "Country")]
        public string Code { get; set; }
        public string Name { get; set; }

        [MongoPropertyValidator(PropertyName="Code")]
        public void CodeIsUp(string CountryCode)
        {
            if (CountryCode != CountryCode.ToUpper())
                throw new Exception(String.Format("{0} must be {1}", CountryCode, CountryCode.ToUpper()));
        }
    }

    [Serializable]
    [MongoKey(KeyFields = "")]
    [MongoIndex(IndexFields = "ID,Country")]
    [MongoIndex(IndexFields =  "Name")]
    [MongoMapperIdIncrementable(IncremenalId = true, ChildsIncremenalId = true)]
    public class Person : MongoMapper
    {        
        public Person()
        {
            Childs = new List<Child>();
        }

        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime BirthDate { get; set; }
        public bool Married { get; set; }
        public decimal BankBalance { get; set; }

        [MongoUpRelation(ObjectName = "Country", FieldName = "Code")]
        public string Country { get; set; }

        [MongoChildCollection]
        public List<Child> Childs { get; set;}
    }

    public class CountryCollection: MongoMapperCollection<Country> {}

Work with the Model

      var c = new Country {Code = "es", Name = "España"};
        try
        {
            c.Save();
        }
        catch (Exception ex)
        {
            Assert.AreEqual(ex.GetBaseException().GetType(), typeof (ValidatePropertyException));
            c.Code = "ES";
            c.Save();
        }

        c = new Country {Code = "UK", Name = "Reino Unido"};
        c.Save();

        c = new Country {Code = "UK", Name = "Reino Unido"};
        try
        {
            c.Save();
        }
        catch (Exception ex)
        {
            Assert.AreEqual(ex.GetBaseException().GetType(), typeof (DuplicateKeyException));
        }

        using (var t = new MongoMapperTransaction())
        {
            var c2 = new Country {Code = "US", Name = "Francia"};
            c2.OnBeforeInsert += (s, e) => { ((Country) s).Name = "Estados Unidos"; };
            c2.Save();

            t.Commit();
        }

        var c3 = new Country();
        c3.FillByKey("US");
        Assert.AreEqual(c3.Name, "Estados Unidos");

        if (!c3.IsLastVersion())
            c3.FillFromLastVersion();

        var countries = new CountryCollection();
        countries.Find();
        Assert.AreEqual(countries.Count, 3);

        countries.Find().SetLimit(2).SetSortOrder("Code");
        Assert.AreEqual(countries.Count, 2);
        Assert.AreEqual(countries.Total, 3);

        countries.Find(
            Query.Or(Query<Country>.EQ(co => co.Code, "ES"), Query<Country>.EQ(co => co.Code, "UK")));
        Assert.AreEqual(countries.Count, 2);

        var p = new Person
            {
                Name = "Pepito Perez",
                Age = 35,
                BirthDate = DateTime.Now.AddDays(57).AddYears(-35),
                Married = true,
                Country = "XXXXX",
                BankBalance = decimal.Parse("3500,00")
            };

        p.Childs.Add(
            new Child {ID = 1, Age = 10, BirthDate = DateTime.Now.AddDays(57).AddYears(-10), Name = "Juan Perez"});

        try
        {
            p.Save();
        }
        catch (Exception ex)
        {
            Assert.AreEqual(ex.GetBaseException().GetType(), typeof (ValidateUpRelationException));
            p.Country = "ES";
            p.Save();
        }

        p.ServerUpdate(
            Update.PushWrapped(
                "Childs",
                new Child {ID = 2, Age = 2, BirthDate = DateTime.Now.AddDays(57).AddYears(-7), Name = "Ana Perez"}));

        var persons = new List<Person>();
        persons.MongoFind("Childs.Age", 2);
        Assert.AreEqual(persons.Count, 1);

You can find examples in the Test Project

Change Log

Something went wrong with that request. Please try again.