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. ...
随机推荐
- Intellij IDEA IDE部署Servlet项目
1.设置Project Structure 2.修改Modules中的Web项目文件默认class编译之后输出位置 3.给Modules中的Web项目添加Web模块 4.修改Web项目Web.xml文 ...
- REST RPC架构思想
1.REST RPC是什么? REST RPC是一个改进版的RPC架构,它是为了解决传统的RPC和REST方案的一些不足之处而生的,它结合了REST API和RPC的优点,同时又克服了REST API ...
- wordpress使用技巧
1.iis6下wordpress去掉index.php 1)安装ISAPIRewritev3.1.0.73 http://bbs.z.admin5.com/forum.php?mod=viewthre ...
- 学习使用:before和:after伪元素
如果你一直密切关注着各种网页设计的博客,你可能已经注意到了:before和:after伪元素已经在前端开发中获得了相当多的关注.特别是在Nicolas Gallagher的博客中,后期运用了很多伪类元 ...
- MFC资源冲突解决方法
AFX_MANAGE_STATE(AfxGetStaticModuleState()) 先看一个例子: 1.创建一个动态链接到MFC DLL的规则DLL,其内部包含一个对话框资源.指定该对话框ID ...
- 从头构建自己的Linux系统
2012-09-10 在博文“Linux系统启动过程分析”中我们了解了linux系统的启动流程,今天我们就来手动一步一步从头来构建一个最小的linux系统,然后用模拟器将其加载起来.常见 ...
- JAVA多线程编程之生产者消费者模式
Java中有一个BlockingQueue可以用来充当堵塞队列,下面是一个桌面搜索的设计 package net.jcip.examples; import java.io.File; import ...
- JavaScript基础知识整理
只整理基础知识中关键技术,旨在系统性的学习和备忘. 1.在 JScript 中 null 和 undefined 的主要区别是 null 的操作象数字 0,而 undefined 的操作象特殊值NaN ...
- maven -- 问题解决(一)解决eclipse中maven项目出现的问题
配置项目时出现的错误: error: Cannot change version of project facet Dynamic Web Module to 2.5. error: One or m ...
- 【算法题】Multiples of 3 and 5
Multiples of 3 and 5 原题 题意如下: 找出N以内的3和5的倍数的和. 思路 1.刚看到觉得好弱智,直接遍历一遍不就OK了吗?但是第2和第3个测试用例报了TLE,超时. 2.然后想 ...