安装配置:

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的更多相关文章

  1. Mongodb在CSharp里实现Aggregate

    回到目录 今天主要用了一个mongodb.driver里的分组,事实上在网上介绍这方面的文章非常少,以至于我在出现问题后,无法找到一个正确的解决方案,最后还是通过异常信息找到的解决方法,所以感觉自己更 ...

  2. c#操作MangoDB 之MangoDB CSharp Driver驱动详解

    序言 MangoDB CSharp Driver是c#操作mongodb的官方驱动. 官方Api文档:http://api.mongodb.org/csharp/2.2/html/R_Project_ ...

  3. MongoDB学习笔记~官方驱动嵌套数组对象的更新

    回到目录 对于数组对象mongodb本身是支持的,不过对于数组的更新,mongodb的Csharp驱动目前只支持一级,即你的对象里包含数组,而数组又包括数组,这表示两层,这在更新子数组时,Csharp ...

  4. 【MongoDB】 基于C#官方驱动2.2版的封装类

    一.前言 最近项目中要用到MongoDB,因此实现做了不少的调研.发现网上很多现有关于MongoDB C#官方驱动的调用方法都是基于1.8版本的,已经不是用了最新的2.2版本.因此我在基于C#官方驱动 ...

  5. Mongodb基本操作说明

    Mongodb基本操作说明 1.首先cmd(管理员方式运行)下启动mongo服务(类似初始化工具): Mongod.exe 默认文件夹为 :c:\data\db 如果没有创建该文件夹的话,需要先创建该 ...

  6. mongodb入门学习小记

    Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...

  7. 2.MongoDB数据库简介

    1).简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  8. asp.net的mongodb实例

    mongodb为2.6版本, .net是4.0, c#接口时1.7. 运行环境为windows8 with visual studio2010 注意事项:在mongodb中使用地理位置存储信息且动用到 ...

  9. 开发基于C#.NET的mongodb桌面版的应用程序(1)

    1.之前没有使用过C#开发过相应的桌面应用程序,现在既然要从零到有进行开发,自然要掌握好C#桌面开发相关的原理与技术,以及站在多类型用户的角度开发具有实际生产意义的mongodb数据库管理软件. 2. ...

随机推荐

  1. 【Cocos2d-Js基础教学 入门目录】

    本教程视地址频在: 九秒课堂 完全免费 从接触Cocos2dx-Js以来,它的绽放的绚丽让我无法不对它喜欢.我觉得Js在不断带给我们惊喜:在开发过程中,会大大提升我们对原型开发的利用率,使用Js语言做 ...

  2. [原]unity5 AssetBundle打包

    本文unity版本5.1.3 一.现有的打包教程: 1.http://liweizhaolili.blog.163.com/blog/static/16230744201541410275298/ 阿 ...

  3. iOS工程集成支付宝错误Undefined symbols for architecture armv7

    问题描述: 新工程中需要集成支付宝功能,于是咱就把支付宝的库给集成了进入然后就出现了下面这种错误了说,错误信息如下: Undefined symbols for architecture armv7: ...

  4. Oracle查找全表扫描的SQL语句

    原文链接:http://blog.itpub.net/9399028/viewspace-678358/ 对于SQL的执行计划,一般尽量避免TABLE ACCESS FULL的出现,那怎样去定位,系统 ...

  5. 配置新系统(Win7 x64)

    新装了一个Win7 x64系统.总结了一些系统配置需要注意的地方. 1. C盘空间 发现C盘被用去了50G的空间,在什么软件都没装的情况下,被用去这么多,感到不可思议. 打开控制面板->文件夹选 ...

  6. 将j-ui(dwz)套用到thinkphp注意事项

    目前我用的 thinkphp 版本是  3.1.3 J-UI  dwz 版本好像是 1.4 现在 j-ui有 thinkphp的例子了,请尽量以他们原创为主,我这里都是一些自己搜集和自己钻研的土办法, ...

  7. 打开jnlp Faild to validate certificate, the application will not be executed.

    今天连jenkins, 本来好好的,只是我在一台机器上一直不断的启动不同的jnlp,绑定不同命名的slave, 然后突然就报错了, 如下截图所示:

  8. HBase 在HDFS 上的目录树

         总所周知,HBase 是天生就是架设在 HDFS 上,在这个分布式文件系统中,HBase 是怎么去构建自己的目录树的呢? 这里只介绍系统级别的目录树. 一.0.94-cdh4.2.1版本 系 ...

  9. Maximum Entropy Markov Models for Information Extraction and Segmentation

    1.The use of state-observation transition functions rather than the separate transition and observat ...

  10. ios 同步Get请求的实现

    //第一步,创建URL NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/search?term=微信&enti ...