Giter Club home page Giter Club logo

mongodb.repository's Introduction

MongoDB.Repository

宗旨:简化MongoDB的操作,实现类似Entity Framework风格的代码

============================================================================================================ 使用方式:

//定义Student类型
public class Student : Entity
{
	[BsonIndex]		//设置索引
	public string Name { get; set; }
	public int Age { get; set; }
}    
//定义Teacher类型
public class Teacher : Entity
{
    	public string Name { get; set; }
    	public int Age { get; set; }
}
//定义RefEntity类型,该类型中含有子集及其操作
public class Grade : RefEntity
{
    	public string Name { get; set; }
}

//定义上下文
public class TestDBContext : MongoDBContext
{
	public TestDBContext() : base("TestDBContext")		//TestDBContext为配置文件中的节点
	{ }

	public override void OnRegisterModel(ITypeRegistration registration)
	{
		registration.RegisterType<Student>().RegisterType<Student>(Teacher).RegisterType<Grade>();
	}
}

//在程序运行伊始,写入如下两段代码进行注册
MongoDBRepository.RegisterMongoDBContext(new TestDBContext());		//注册上下文
MongoDBRepository.RegisterMongoIndex();					//注册索引

//配置文件中的MongoDB连接字符串节点
<configuration>
	<connectionStrings>
		<add name="TestDBContext" connectionString="mongodb://localhost:27017/TestMongo"/>
	</connectionStrings>
</configuration>

//Entity使用
//单实体保存
Student student = new Student()
student.Name = "hyf";
student.Age = 30;
student.Save();

//集合保存
MongoEntity.Save(new List<Student>() {
    new Student{ Name="hyf", Age=33 },
    new Student{ Name="zhc", Age=30 }
});

//查询
MongoEntity.Get<Student>(student.Id);
MongoEntity.Get<Student>(s => s.Name == "hyf" && s.Age > 33);
MongoEntity.Select<Student>(s => s.Age == 30).ToList();
MongoEntity.Select<Student>(s => s.Age >= 19 && s.Age <= 22, s => s.Age, pageIndex=1, pageSize=2, out pageCount, out allCount).ToList();

//删除
MongoEntity.RemoveAll<Student>(e => e.Name == "hyf");

//统计
MongoEntity.Count<Student>(s => s.Age == 30)

//更多操作请参考MongoEntity

//=================================================================================================================
//RefEntity使用
//保存
grade = new Grade();
grade.Name = "No1";
foreach (Student student in students)
    grade.Add<Student>(student);
foreach (Teacher teacher in teachers)
    grade.Add<Teacher>(teacher);
grade.Update();		//保存grade实例及其子集,如使用grade.save(),则只保存grade实例,不保存子集。

//查询子集
grade.Pick<Student>("BsonId string").Name

//统计子集
grade.Count<Student>()	//统计当前子集中的Student类型

//更多关于自己的操作如查询,判断,删除等请参考IRefEntity接口

=================================================================================================================

Auther: WinHu

Blog: http://www.cnblogs.com/winhu/

欢迎大家参与并指正,提出更好的意见。

mongodb.repository's People

Contributors

winhu avatar

Watchers

James Cloos 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.