简介:本文仅提供快速入门级别的使用C# Driver操作MongoDB,高手跳过

  1. Downloading the C# Driver
    1. 猛击下载
  2. 添加相关的dll引用
        MongoDB.Bson.dll
    MongoDB.Driver.dll
  3. 添加名称空间引用
    using MongoDB.Bson;
    using MongoDB.Driver;
  4. 获取客户端对象
    var connectionString = "mongodb://localhost";
    var client = new MongoClient(connectionString);
  5. 获取服务端对象
    var server = client.GetServer();
  6. 获取要操作的数据库
    var database = server.GetDatabase("test"); // "test" is the name of the database
  7. CRUD(使用自定义的类)
    1. 自定义实体

      public class Entity
      {
      public ObjectId Id { get; set; } public string Name { get; set; }
      }
    2. 获取要操作的表
      // "entities" is the name of the collection
      var collection = database.GetCollection<Entity>("entities");
    3. 新增一条记录
      var entity = new Entity { Name = "Tom" };
      collection.Insert(entity);
      var id = entity.Id; // Insert will set the Id if necessary (as it was in this example)
    4. 查询一条记录
      var query = Query<Entity>.EQ(e => e.Id, id);
      var entity = collection.FindOne(query);
    5. 保存一条记录(发送整个实体到数据库)
      entity.Name = "Dick";
      collection.Save(entity);
    6. 修改一条记录(仅发送修改的部分到数据库,这一点是和保存还是有区别的,根据场景来自行判断需要用哪一种来更新数据)
      var query = Query<Entity>.EQ(e => e.Id, id);
      var update = Update<Entity>.Set(e => e.Name, "Harry"); // update modifiers
      collection.Update(query, update);
    7. 删除一条记录
      var query = Query<Entity>.EQ(e => e.Id, id);
      collection.Remove(query);

完整演示代码:

 using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks; namespace MongoDBTest
{
class Program
{
static void Main(string[] args)
{
var connectionString = "mongodb://localhost:27017";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("test");
var collection = database.GetCollection<Entity>("entities");
var entity = new Entity { Name = "Tom" };
var i = collection.Insert(entity);
var id = entity.Id; var query = Query<Entity>.EQ(e => e.Id, id);
entity = collection.FindOne(query);
entity.Name = "Dick";
var s = collection.Save(entity); var update = Update<Entity>.Set(e => e.Name, "Harry");
collection.Update(query, update); collection.Remove(query); Console.ReadKey(); } }
public class Entity
{
public ObjectId Id;
public string Name { get; set; }
} }

MongoDB-Getting Started with the C# Driver的更多相关文章

  1. PHP连接mongodb的现代用法---使用Monogodb\Driver\Manager

    目的:在php程序端查询文档相关集合存储情况 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/11/29 ...

  2. 9.6 MongoDB一

    目录:ASP.NET MVC企业级实战目录 9.6.1 MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统 ...

  3. dotnet core 使用 MongoDB 进行高性能Nosql数据库操作

    好久没有写过Blog, 每天看着开源的Java社区流口水, 心里满不是滋味. 终于等到了今年六月份 dotnet core 的正式发布, 看着dotnet 社区也一步一步走向繁荣, 一片蒸蒸日上的大好 ...

  4. MongoDB做为一项windows服务启动

    MongoDB做为一项windows服务启动 Windows版本安装 MongoDB的官方下载站是http://www.mongodb.org/downloads,可以去上面下载最新的对应版本,有32 ...

  5. MongoDB安装及入门

    下载 windows下的是3.2的版本 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ mongodb采用27 ...

  6. Windows7+32位,MongoDB安装

    在网上找了各种安装MongoDB的教程,总是会出现一些bug,就自己总结了,亲测正确,MongoDB的安装再也不是一件麻烦的事情了~ 1.下载好跟自己电脑适合的安装包,选择Custom自定义安装,将安 ...

  7. node与mongodb的使用

    1.mongdb安装好后,在.bin文件夹下执行mongod.exe --dbpath=D:\mongodb\db  即可启动mongodb,表示将数据放在db这个文件夹,且每一次启动要执行完整的这句 ...

  8. 记录在windows7上安装MongoDB

    1.首先下载   官网地址  https://www.mongodb.com/download-center#community 选择 Windows Vista 32-bit, without SS ...

  9. MongoDB安装与启动

    我本人电脑是win8系统64位,下载64位的zip包,下载完成后解压缩到D:\MongoDB目录 创建数据库目录D:\MongoDB\data,接下来打开命令行窗口,切换到D:\MongoDB\bin ...

  10. mongodb 安装与启动简单使用

    环境:mac 10.11.6 一.安装步骤:按照官网的教程: 1.打开终端 安装或升级brew: brew update 2.安装mongoDB二进制文件: brew install mongodb ...

随机推荐

  1. Python之路【第七篇】:初识Socket

    What is Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket的英文原义是“孔”或“插座”.作为BSD UNIX的进程通信机制, ...

  2. OC-改错题

    1,类方法中不能访问成员变量 2,id后不能加*(因为id相当于NSObject *) 3,id类型的变量不能用点语法 4,类对象只能调用类方法,不能调用对象方法 .description #impo ...

  3. hive odbc

    C:\Windows\SysWOW64  

  4. java 递归获取一个目录下的所有文件路径

    还是日志的问题,log4j生成的日志文件,自动保存到月份所在的文件夹中,需要获取到所有的日志文件,包括文件夹 private List<String> ergodic(File file, ...

  5. asp.net mvc 4 高级编程学习笔记:第三章 视图(2)

    页面布局 asp.net MVC中提供了布局的支持,默认情况下才布局文件保存到 /View/Shared/目录下的_Layout.cshtml,View目录有个_ViewStart.cshtml文件, ...

  6. PHP常用的一些正则表达式

    附一些常用的正则运算: 验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0- ...

  7. 繁华模拟赛day8 科技树

    /* 贪心,很明显是越容易升级的越先升级 */ #include<iostream> #include<cstdio> #include<string> #incl ...

  8. 欧几里得证明$\sqrt{2}$是无理数

    选自<费马大定理:一个困惑了世间智者358年的谜>,有少许改动. 原译者:薛密 \(\sqrt{2}\)是无理数,即不能写成一个分数.欧几里得以反证法证明此结论.第一步是假定相反的事实是真 ...

  9. R入门<二>-时间序列研究

    续之前那篇随笔 前天写完随笔后,很自豪的拿出来去跟带我入数据挖掘和SAS基础的大牛@八公炫耀,然后收获了一堆时间序列的材料,非常感谢大牛! ARIMA就是看图形,ACF和PACF,原理不需要知道,因为 ...

  10. PHP array_multisort() 函数详解 及 二维数组排序(模拟数据表记录按字段排序)

    一.先看最简单的情况. 有两个数组: $arr1 = array(1, 9, 5); $arr2 = array(6, 2, 4); array_multisort($arr1, $arr2); pr ...