简介:本文仅提供快速入门级别的使用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. Java多线程编程核心技术--Lock的使用(一)

    使用ReentrantLock类 在Java多线程中,可以使用synchronized关键字来实现线程之间的同步互斥,但在JDK1.5中新增加了ReentrantLock类也能达到同样的效果,并且在扩 ...

  2. Java继承和多态实例

    我们知道面向对象的三大特性是封装.继承和多态.然而我们有时候总是搞不清楚这些概念.下面对这些概念进行整理, 为以后面向抽象的编程打下坚实的基础. 封装的概念还是很容易理解的.如果你会定义类,那么相信你 ...

  3. linuxMint下安装ftp工具--filezilla

    windows下ftp工具有好多,linux下推荐用filezilla 安装filezilla很简单,安装完后,使用方式和windows下面一样. 第一种方式: 直接去filezilla官网下载软件包 ...

  4. 工作中一个简单的shell程序

    下面是工作中用到的链接数据库的shell程序. #!/bin/bash ] ; then echo "prase is wrong ,please check first" exi ...

  5. php正则表达式匹配用户名规则:由字母开头的6-16位字母和数字组成的字符串

    $pattern = '/^[a-zA-Z]{1}[a-zA-Z0-9]{5,15}$/i';

  6. ConcurrentHashMap-----不安全线程hashmap-安全线程-hashtable

    JDK1.0引入了第一个关联的集合类HashTable,它是线程安全的.HashTable的所有方法都是同步的.JDK2.0引入了HashMap,它提供了一个不同步的基类和一个同步的包装器synchr ...

  7. GOF业务场景的设计模式-----单例模式

    个人觉得 纯粹的学习设计模式,是不对的.也不能为了使用设计模式,而硬搬设计模式来使用 单例模式可能是 最简单的设计模式也是 大家知道最多的设计模式.当然 ,有很多种写法 定义:确保一个类只有一个实例, ...

  8. 成功的背后!(给所有IT人)

    转载:来自CSDN第一名博主:http://blog.csdn.net/phphot/article/details/2187505 成功的背后,有着许多不为人知的故事,而正是这些夹杂着泪水和汗水的过 ...

  9. [译]Mongoose指南 - 中间件

    中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生. 中间件都是document级别的不是model级别的. 下面讲讲两种中间件pre ...

  10. Linux中的运行级

    1. Linux系统有7个运行级别(runlevel): 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆 ...