安装配置:

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. [原] JsTree.js

    写自用软件系统时查找到的树列表控件过于庸余,样式难调,故自写一套完整的简易js_TreeTable控件,使用时简单的添加自定义的样式效果即可,特此发布第一个版本. 源码如下: /* * Huashan ...

  2. (笔记)Linux内核学习(九)之内核内存管理方式

    一 页 内核把物理页作为内存管理的基本单位:内存管理单元(MMU)把虚拟地址转换为物理 地址,通常以页为单位进行处理.MMU以页大小为单位来管理系统中的也表. 32位系统:页大小4KB 64位系统:页 ...

  3. jQuery插件:模拟select下拉菜单

    没搞那么复杂,工作中,基本够用.. <!doctype html> <html> <head> <meta charset="utf-8" ...

  4. Java IO 之 OutputStream源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  5. 牢骚与javascript中的this

    最近在看关于拖延症的一本书<拖拉一点也无妨>,后面得出结论是自己写博客大部分处于两种状态,心情很好和心情很不好的时候.因为正常状态下感觉写博客吧,是件很麻烦的事情,不如去看看电影看看漫画啥 ...

  6. SQLServer创建维护计划失败 错误c001f011

    重新注册dts.dll文件,在运行里输入命令:(x64)regsvr32 "C:\Program Files\Microsoft SQL Server\100\DTS\Binn\dts.dl ...

  7. asp.net mvc 利用过滤器进行网站Meta设置

    过去几年都是用asp.net webform进行开发东西,最近听说过时了,同时webform会产生ViewState(虽然我已经不用ruanat=server的控件好久了 :)),对企业应用无所谓,但 ...

  8. Qt 添加资源文件

    *本人乃小白,博文主要用于个人记录,不保证内容准确无误* 我们编写的gui可能需要一些额外的资源(比如贴图用的图片),可用资源文件统一管理.以下以图片为例. 用qt creator 打开工程,为工程新 ...

  9. Qt5 从头学(1)-- 环境

    对我来说MFC太过麻烦了,同样是桌面开发工具,Qt就完全不一样了.Qt使用C++语言可以轻松实现"一次编写,到处编译"的跨平台性能,并且可以做出很多炫酷的界面效果.目前支持几乎所有 ...

  10. 网络通信分享(二):外网ip和内网ip

    一.内网ip包括两类: 1:tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255  172.16.0.0/ ...