Skip to content

SkyChenSky/Sikiro.Nosql.Mongo

Repository files navigation

Sikiro.Nosql.Mongo 中文

This is mongo repository.Base on MongoDB.Driver.It is easy to use.

Getting Started

Nuget

You can run the following command to install the Sikiro.Nosql.Mongo in your project。

PM> Install-Package Sikiro.Nosql.Mongo

Connection

var mongoRepository = new MongoRepository("mongodb://10.1.20.143:27017");

Defining User Entity

[Mongo("Chengongtest", "User")]
public class User : MongoEntity
{
    public string Name { get; set; }

    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
    public DateTime BirthDateTime { get; set; }

    public User Son { get; set; }

    public int Sex { get; set; }

    public List<string> AddressList { get; set; }
}

Or no user MongoAttribute and set default database name for many repository such as:

public class UserRepository : MongoRepository
{
    public UserRepository(string connectionString) : base(connectionString, "chengongtest")
    {
    }
}

public class User : MongoEntity
{
    public string Name { get; set; }

    [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
    public DateTime BirthDateTime { get; set; }

    public User Son { get; set; }

    public int Sex { get; set; }

    public List<string> AddressList { get; set; }
}

var userRepository = new UserRepository(url);

Add

var addresult = mongoRepository.Add(new User
{
    Name = "skychen",
    BirthDateTime = new DateTime(1991, 2, 2),
    AddressList = new List<string> { "guangdong", "guangzhou" },
    Sex = 1,
    Son = new User
    {
        Name = "xiaochenpi",
        BirthDateTime = DateTime.Now
    }
});

UPDATE

Update according to the condition part field

mongoRepository.Update<User>(a => a.Id == u.Id, a => new User { AddressList = new List<string> { "guangdong", "jiangmen", "cuihuwan" } });

You can also update the entity field information based on the primary key

getResult.Name = "superskychen";
mongoRepository.Update(getResult);

Arrary Action

add one no repeat item

mongoRepository.Update<User>(a => a.Id == u.Id, a => new User
            {
                AddressList = a.AddressList.AddToSet("skychen")
            });

delete one of array item

mongoRepository.Update<User>(a => a.Id == u.Id, a => new User
            {
                AddressList = a.AddressList.Pull("skychen")
            });

add one item

mongoRepository.Update<User>(a => a.Id == u.Id, a => new User
            {
                AddressList = a.AddressList.Push("skychen")
            });

DELETE

Delete according to the condition

mongoRepository.Delete<User>(a => a.Id == u.Id);

QUERY

GET

Get the first data by filtering condition

var getResult = mongoRepository.Get<User>(a => a.Id == u.Id);

TOLIST

You can also query qualified data list.

var listResult = mongoRepository.ToList<User>(a => a.Id == u.Id);

PAGELIST

var listResult = mongoRepository.PageList<User>(a => a.Id == u.Id, a => a.Desc(b => b.BirthDateTime), 1, 10);

Finally a complete Demo

var url = "mongodb://10.1.20.143:27017";
var mongoRepository = new MongoRepository(url);

var u = new User
{
    Name = "skychen",
    BirthDateTime = new DateTime(1991, 2, 2),
    AddressList = new List<string> { "guangdong", "guangzhou" },
    Sex = 1,
    Son = new User
    {
        Name = "xiaochenpi",
        BirthDateTime = DateTime.Now
    }
};

var addresult = mongoRepository.Add(u);

var getResult = mongoRepository.Get<User>(a => a.Id == u.Id);
getResult.Name = "superskychen";

mongoRepository.Update(getResult);

mongoRepository.Update<User>(a => a.Id == u.Id, a => new User { AddressList = new List<string> { "guangdong", "jiangmen", "cuihuwan" } });

mongoRepository.Exists<User>(a => a.Id == u.Id);

mongoRepository.Delete<User>(a => a.Id == u.Id);

Others

In addition to the above functions, there are aggregated queries.Such as Count、Sum、Exists

End

If you have good suggestions, please feel free to mention to me.

About

Easy to use mongo repository

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages