简介:本文仅提供快速入门级别的使用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. Logback LogBack

    1.简介 LogBack是一个日志框架,它与Log4j可以说是同出一源,都出自Ceki Gülcü之手.(log4j的原型是早前由Ceki Gülcü贡献给Apache基金会的) 1.1 LogBac ...

  2. python 文件包含

    Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里 ...

  3. php-fpm进程数优化方法

    原文地址:https://www.douban.com/note/315222037/ 背景最近将Wordpress迁移至阿里云.由于自己的服务器是云服务器,硬盘和内存都比较小,所以内存经常不够使,通 ...

  4. oracle中一些用法总结

    1. case用法: --简单case函数 case sex when '1' then 'boy' when '2' then 'girl' else '其他' end; --case搜索函数 ca ...

  5. Web渗透测试使用Kali Linux(一)渗透测试概要及环境部署

    渗透测试是利用已经发现的漏洞,采用恶意黑客的惯用手段来尝试对漏洞进行攻击. Kali Linux是BackTrack的进化版,是Linux的衍生版本,专门开发用作渗透测试,其中提供了很多的渗透测试工具 ...

  6. WCF中安全的那些事!!!

    WCF默认绑定 WCF预先为所有绑定都定义了能满足大多数情形的配置模式,这样的话,只要没有修改某个配置参数,WCF就使用默认的安全模式. 先上一张默认的安全设置表格 绑定 设置 wsHttpBindi ...

  7. [译]git commit

    git commit git commit命令提交stage区的快照到项目历史中去(HEAD). 被提交的快照被认为是一个项目的安全版本. Git不会修改他们, 除非你显示的要求了. 和git add ...

  8. [译]bare repository

    git init --bare 使用--bare创建的repository没有工作目录, 在这个repository中不能修改文件和commit. 中心repository必须是bare reposi ...

  9. Perl 正则表达式

    匹配:m/<regexp>;/ (还可以简写为 /<regexp>;/ ,略去 m)替换:s/<pattern>;/<replacement>;/转化: ...

  10. sql 2005,2008开启bcp的方法嗯哈步骤

    sqlserver 2008开启bcp服务的方法和步骤 sqlserver 2005开启bcp服务的方法和步骤 在开始菜单中找到sql server 2005 -->> 配置工具 --&g ...