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. ...
随机推荐
- c++算法应用 预备
章 C + +程序设计 大家好!现在我们将要开始一个穿越" 数据结构.算法和程序" 这个抽象世界的特殊旅程,以解决现实生活中的许多难题.在程序开发过程中通常需要做到如下两点:一是高 ...
- 申请Payoneer美国万事达信用卡,可获得一个美国虚拟银行账户,立即注册可得25美元
申请Payoneer美国万事达信用卡,可获得一个美国虚拟银行账户,可以在国内任意一个支持万事达的ATM.POS机上取现和刷卡消费.Payoneer可以网上购物,购买国外的产品,对我们有一个好处就是利用 ...
- Solr4:数据导入(dataimport)时,不符合Solr日期类型要求的字段的处理
背景: 要求将一个SQL Server2012版本中的数据库导入到Solr中.数据表中有一字段用来存储birthday日期字段,为nvarchar类型,长度为8,格式为:yyyyMMdd. 导入Sol ...
- 调用 webapi的put和delete 报"Method Not Allowed" 405 错误。
修改引用到webapi的Dll文件对应的项目的web.config 选择生成读写方法webapi会生成四个读写的方法(CRUD),两个获取数据的.一个更新.一个删除,默认情况下更新和删除是不对外开外的 ...
- SQL 存储过程入门(事务)(四)
SQL 存储过程入门(事务)(四) 本篇我们来讲一下事务处理技术. 为什么要使用事务呢,事务有什么用呢,举个例子. 假设我们现在有个业务,当做成功某件事情的时候要向2张表中插入数据,A表,B表,我 ...
- cocos2dx 帧动画的两种创建方式
看了好几天cocos2dx的帧动画,现在才有点眉目,为了高效期间我们一般会用到 精灵帧缓存(CCSpriteFrameCache) 和动画缓存(CCAnimationCache) .大体的操作步骤: ...
- java--遍历自定义数组
比如像下面这样 for (int i : new int[]{1,4,8}){ System.out.println(i); } 或者这样: for (String i : new String[]{ ...
- VMware三个版本workstation、server、esxi的区别
VMware三个版本 workstation: 单机级,用在个人桌面系统中,需要操作系统支持 servier:工作组级,用于服务器,需要操作系统支持 esxi:企业级,用于服务器,不需要操作系统支持 ...
- linux下批量查找/替换文本内容
一般在本地电脑上批量替换文本有许多工具可以做到,比如sublime text ,但大多服务器上都是无图形界面的,为此收集了几条针对linux命令行 实现批量替换文本内容的命令: 1.批量查找某个目下文 ...
- Xenia and Bit Operations(线段树单点更新)
Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...