csharp: MongoDB
安装配置:
Install MongoDB on Windows(安装配置官方参考) http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
1.Run MongoDB
C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe --dbpath d:\data\db
#配置数据库
mongod.exe --dbpath d:\data\db
#配置日志文件
mongod.exe --logpath D:\data\logs\mongodb.log --install
#测试用户登录
mongo -u geovindu -p
2.C# 连接字符串
<!--<add key="connectionString" value="Server=localhost:27017"/>-->
<!--<add key="connectionString" value="mongodb://localhost:27017"/>-->
<!--<add key="connectionString" value="Server=127.0.0.1:27017"/>-->
<add key="connectionString" value="mongodb://127.0.0.1:27017"/>
以上四项都可以
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using MongoDB.Bson;
using MongoDB.Driver; namespace MongoDB2.Model
{
/// <summary>
/// Wrapper class to communicate with 'MyCompany' database.
/// </summary>
class MyCompany
{ /// <summary>
///
/// </summary>
public MyCompany()
{ } /// <summary>
/// Connection string to the Mongo database server
/// </summary>
public static string ConnectionString
{
get
{
return ConfigurationManager.AppSettings["connectionString"];
}
} /// <summary>
/// Creates sample data for two collections(or tables) i.e, Departments, Employees.
/// </summary>
public static void CreateData()
{
CreateDepartments();
CreateEmployees();
} #region Departments /// <summary>
/// Retrieve departments from MyCompany database.
/// </summary>
/// <returns></returns>
public static List<Department> GetDepartments()
{
List<Department> lst = new List<Department>(); MongoServer server = MongoServer.Create(ConnectionString);
MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
MongoDatabase myCompany = server.GetDatabase("geovinDB");//,credentials//MyCompany
MongoCollection<Department> departments = myCompany.GetCollection<Department>("Departments");
foreach (Department department in departments.FindAll())
{
lst.Add(department);
} return lst;
} /// <summary>
/// Inserts sample departments data in MyCompany database
/// </summary>
private static void CreateDepartments()
{
string headOfDepartmentId; //insert department 'Development'
headOfDepartmentId = "4f180083ef31ba0da8000010";
CreateDepartment("Development", headOfDepartmentId); //insert department 'Accounts'
headOfDepartmentId = "4f180083ef31ba0da8000011";
CreateDepartment("Accounts", headOfDepartmentId); //insert department 'Human Resource'
headOfDepartmentId = "4f180083ef31ba0da8000012";
CreateDepartment("Human Resource", headOfDepartmentId);
} /// <summary>
/// Insert the department
/// </summary>
/// <param name="departmentName"></param>
/// <param name="headOfDepartmentId"></param>
private static void CreateDepartment(string departmentName, string headOfDepartmentId)
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials //MyCompany MongoCollection<BsonDocument> departments = myCompany.GetCollection<BsonDocument>("Departments");
BsonDocument deptartment = new BsonDocument {
{ "DepartmentName", departmentName },
{ "HeadOfDepartmentId", headOfDepartmentId }
}; departments.Insert(deptartment);
} /// <summary>
/// Delete all data in departments collection in MyCompany database
/// </summary>
public static void DeleteDepartments()
{
MongoServer server = MongoServer.Create(ConnectionString); MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//MyCompany MongoCollection<Department> departments = myCompany.GetCollection<Department>("Departments");
departments.Drop();
}
#endregion #region Employees /// <summary>
/// Retrieve employees from MyCompany database.
/// </summary>
/// <returns></returns>
public static List<Employee> GetEmployees()
{
List<Employee> lst = new List<Employee>(); MongoServer server = MongoServer.Create(ConnectionString);
MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//无验证密码登录 MongoCollection<Employee> employees = myCompany.GetCollection<Employee>("Employees");
foreach (Employee employee in employees.FindAll())
{
lst.Add(employee);
} return lst;
} /// <summary>
/// Inserts sample employees data in MyCompany database
/// </summary>
private static void CreateEmployees()
{
// add 5 sample Employees
for (int i = 1; i <= 5; i++)
{
string departmentId = "4f180083ef31ba0da8000010";
CreateEmployee("FirstName" + i, "LastName" + i, "Address" + i, "City" + i, departmentId);
}
} /// <summary>
/// Insert the employee
/// </summary>
/// <param name="departmentName"></param>
/// <param name="headOfDepartmentId"></param>
private static void CreateEmployee(string firstName, string lastName, string address, string city, string departmentId)
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//MyCompany MongoCollection<BsonDocument> employees = myCompany.GetCollection<BsonDocument>("Employees");
BsonDocument employee = new BsonDocument {
{ "FirstName", firstName },
{ "LastName", lastName },
{ "Address", address },
{ "City", city },
{ "DepartmentId", departmentId }
}; employees.Insert(employee);
} /// <summary>
/// Delete all data in employees collection in MyCompany database
/// </summary>
public static void DeleteEmployees()
{
MongoServer server = MongoServer.Create(ConnectionString);
MongoCredentials credentials = new MongoCredentials("geovindu", "geovindu");
MongoDatabase myCompany = server.GetDatabase("geovinDB");//, credentials//MyCompany MongoCollection<Employee> employees = myCompany.GetCollection<Employee>("Employees");
employees.Drop();
}
#endregion
} #region Department
/// <summary>
/// Department represents a single item(record) stored in Departments collection.
/// </summary>
class Department
{
public ObjectId _id { get; set; }
public string DepartmentName { get; set; }
public ObjectId HeadOfDepartmentId { get; set; }
}
#endregion #region Employee
/// <summary>
/// Department represents a single item(record) stored in Employees collection.
/// </summary>
class Employee
{
public ObjectId _id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public ObjectId DepartmentId { get; set; }
}
#endregion
}
csharp: MongoDB的更多相关文章
- Mongodb在CSharp里实现Aggregate
回到目录 今天主要用了一个mongodb.driver里的分组,事实上在网上介绍这方面的文章非常少,以至于我在出现问题后,无法找到一个正确的解决方案,最后还是通过异常信息找到的解决方法,所以感觉自己更 ...
- c#操作MangoDB 之MangoDB CSharp Driver驱动详解
序言 MangoDB CSharp Driver是c#操作mongodb的官方驱动. 官方Api文档:http://api.mongodb.org/csharp/2.2/html/R_Project_ ...
- MongoDB学习笔记~官方驱动嵌套数组对象的更新
回到目录 对于数组对象mongodb本身是支持的,不过对于数组的更新,mongodb的Csharp驱动目前只支持一级,即你的对象里包含数组,而数组又包括数组,这表示两层,这在更新子数组时,Csharp ...
- 【MongoDB】 基于C#官方驱动2.2版的封装类
一.前言 最近项目中要用到MongoDB,因此实现做了不少的调研.发现网上很多现有关于MongoDB C#官方驱动的调用方法都是基于1.8版本的,已经不是用了最新的2.2版本.因此我在基于C#官方驱动 ...
- Mongodb基本操作说明
Mongodb基本操作说明 1.首先cmd(管理员方式运行)下启动mongo服务(类似初始化工具): Mongod.exe 默认文件夹为 :c:\data\db 如果没有创建该文件夹的话,需要先创建该 ...
- mongodb入门学习小记
Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...
- 2.MongoDB数据库简介
1).简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...
- asp.net的mongodb实例
mongodb为2.6版本, .net是4.0, c#接口时1.7. 运行环境为windows8 with visual studio2010 注意事项:在mongodb中使用地理位置存储信息且动用到 ...
- 开发基于C#.NET的mongodb桌面版的应用程序(1)
1.之前没有使用过C#开发过相应的桌面应用程序,现在既然要从零到有进行开发,自然要掌握好C#桌面开发相关的原理与技术,以及站在多类型用户的角度开发具有实际生产意义的mongodb数据库管理软件. 2. ...
随机推荐
- 【Cocos2d-Js基础教学 入门目录】
本教程视地址频在: 九秒课堂 完全免费 从接触Cocos2dx-Js以来,它的绽放的绚丽让我无法不对它喜欢.我觉得Js在不断带给我们惊喜:在开发过程中,会大大提升我们对原型开发的利用率,使用Js语言做 ...
- Swift XML解析库 - SwiftyXMLParser
经过在CocoaPods中筛选以后,发这个这个比较好用,整理出来 如果有需要可以在Pods命令端输入: pod search xml 这样会搜索出很多相关Xml的第三方库 SwiftyXMLParse ...
- .NET Framework 高级开发
.NET Framework 高级开发 MSDN 这部分帮助介绍与 .NET Framework 相关的高级编程主题. 本节内容 管理 介绍如何管理 Active Directory 中的对象,如何使 ...
- 译:在ASP.NET中如何对cookies进行加密和解密
译文地址:http://www.codeproject.com/Tips/872826/Encrypt-Decrypt-Cookies-in-ASP-NET 源代码:http://files.cnbl ...
- LevelDB(v1.3) 源码阅读之 Arena(内存管理器)
LevelDB(v1.3) 源码阅读系列使用 LevelDB v1.3 版本的代码,可以通过如下方式下载并切换到 v1.3 版本的代码: $ git clone https://github.com/ ...
- 自定义组件之MoreListView
前言 本文针对自定义组件进行一些分析.还是那句老话“授之于鱼不如授之以渔”.今天要讲的是一个自定义的可以分页的ListView. 网上都讲了些ListView分页的方法,那么为什么我在这里还需要自己写 ...
- Guardian of Decency(二分图)
Guardian of Decency Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- android.support.v4包中的LruCache源码简读
package android.util; import java.util.LinkedHashMap; import java.util.Map; /** * A cache that holds ...
- (转)linux内核虚拟文件系统浅析
转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 ###### 虚拟文件系统(VFS)在我看来, "虚拟"二字主要 ...
- C#结构体的特点浅析
C#结构体的特点浅析 2009-08-13 11:18 意识的偏差 百度空间 字号:T | T C#结构体的特点有哪些呢?C#结构体与类的区别是什么呢?本文就向你介绍相关的内容. AD: C# ...