MongoDB-Getting Started with the C# Driver
简介:本文仅提供快速入门级别的使用C# Driver操作MongoDB,高手跳过
- Downloading the C# Driver
- 添加相关的dll引用
MongoDB.Bson.dll
MongoDB.Driver.dll - 添加名称空间引用
using MongoDB.Bson;
using MongoDB.Driver; - 获取客户端对象
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString); - 获取服务端对象
var server = client.GetServer();
- 获取要操作的数据库
var database = server.GetDatabase("test"); // "test" is the name of the database - CRUD(使用自定义的类)
- 自定义实体
public class Entity
{
public ObjectId Id { get; set; } public string Name { get; set; }
} - 获取要操作的表
// "entities" is the name of the collection
var collection = database.GetCollection<Entity>("entities"); - 新增一条记录
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) - 查询一条记录
var query = Query<Entity>.EQ(e => e.Id, id);
var entity = collection.FindOne(query); - 保存一条记录(发送整个实体到数据库)
entity.Name = "Dick";
collection.Save(entity); - 修改一条记录(仅发送修改的部分到数据库,这一点是和保存还是有区别的,根据场景来自行判断需要用哪一种来更新数据)
var query = Query<Entity>.EQ(e => e.Id, id);
var update = Update<Entity>.Set(e => e.Name, "Harry"); // update modifiers
collection.Update(query, update); - 删除一条记录
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的更多相关文章
- PHP连接mongodb的现代用法---使用Monogodb\Driver\Manager
目的:在php程序端查询文档相关集合存储情况 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/11/29 ...
- 9.6 MongoDB一
目录:ASP.NET MVC企业级实战目录 9.6.1 MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统 ...
- dotnet core 使用 MongoDB 进行高性能Nosql数据库操作
好久没有写过Blog, 每天看着开源的Java社区流口水, 心里满不是滋味. 终于等到了今年六月份 dotnet core 的正式发布, 看着dotnet 社区也一步一步走向繁荣, 一片蒸蒸日上的大好 ...
- MongoDB做为一项windows服务启动
MongoDB做为一项windows服务启动 Windows版本安装 MongoDB的官方下载站是http://www.mongodb.org/downloads,可以去上面下载最新的对应版本,有32 ...
- MongoDB安装及入门
下载 windows下的是3.2的版本 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ mongodb采用27 ...
- Windows7+32位,MongoDB安装
在网上找了各种安装MongoDB的教程,总是会出现一些bug,就自己总结了,亲测正确,MongoDB的安装再也不是一件麻烦的事情了~ 1.下载好跟自己电脑适合的安装包,选择Custom自定义安装,将安 ...
- node与mongodb的使用
1.mongdb安装好后,在.bin文件夹下执行mongod.exe --dbpath=D:\mongodb\db 即可启动mongodb,表示将数据放在db这个文件夹,且每一次启动要执行完整的这句 ...
- 记录在windows7上安装MongoDB
1.首先下载 官网地址 https://www.mongodb.com/download-center#community 选择 Windows Vista 32-bit, without SS ...
- MongoDB安装与启动
我本人电脑是win8系统64位,下载64位的zip包,下载完成后解压缩到D:\MongoDB目录 创建数据库目录D:\MongoDB\data,接下来打开命令行窗口,切换到D:\MongoDB\bin ...
- mongodb 安装与启动简单使用
环境:mac 10.11.6 一.安装步骤:按照官网的教程: 1.打开终端 安装或升级brew: brew update 2.安装mongoDB二进制文件: brew install mongodb ...
随机推荐
- HTML5播放器实例
鉴于html5Audio and video的使用,设计了一个自定义风格的播放器,除实现一些基本的默认功能之外,还实现了一些高级功能. 具体功能如下: 实现播放暂停按钮 实现静音按钮 实现音量调节滑动 ...
- linux标准输入输出及错误输出
Linux Shell 环境中支持输入输出重定向,用符号"<"和">"来表示. 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定 ...
- Cobbler自动化批量部署CentOS6.5系统
Cobbler作为一个预备工具,使批量部署Red Hat/Centos/Fedora系统更容易,同时也支持Suse和Debian系统的部署. 它提供以下服务集成: * PXE服务支持 * DHCP服务 ...
- python中os/sys/platform模块区别
os:This module provides a portable way of using operating system dependent functionality. sys:This m ...
- QT读写ini配置文件
/********下面是写ini文件*************************/ //Qt中使用QSettings类读写ini文件 //QSettings构造函数的第一 ...
- C语言打乱一组数字顺序
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<time.h> int m ...
- Double和double的区别
1.Double是java定义的类,而double是预定义数据类型(8种中的一种)2.Double就好比是对double类型的封装,内置很多方法可以实现String到double的转换,以及获取各种d ...
- 将Ubuntu 15.10升级到Ubuntu 16.04
Ubuntu 16.04 LTS 代号为 Xenial Xerus,其最终版将于 2016 年 4 月 21 日正式发布,Ubuntu16.04 将是非常受欢迎的开源操作系统 Ubuntu 的第 6 ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- PHP获取MAC地址的函数代码
获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 复制代码 代码如下: <?php /** 获取网卡的MAC地址原码:目前支持WIN/LINUX系统 ...