Git Product home page Git Product logo

sikiro.nosql.mongo's Introduction

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.

sikiro.nosql.mongo's People

Contributors

skychensky avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.