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 ...
随机推荐
- Thread 与 Runnable
在Java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...
- cookie, localStorage, sessionStorage区别
cookie 有过期时间,默认是关闭浏览器后失效,4K,兼容ie6,不可跨域,子域名会继承父域名的cookielocalStorage 永不过期,除非手动删除,5M,兼容IE8,不可跨域,子域名不能继 ...
- 开源项目管理平台*redmine*的架设
yum -y install ruby yum install rubygems gem install heroku gem install rack -v=1.0.1 gem install ru ...
- html、css杂记
1:浮动 <div style="float: left"> 2:清除浮动,把父div撑起来 <div style="clear:both"& ...
- Jetty多Connector
有时候想要启动两个端口,或者通过一个Jetty server提供多个不同服务,比如说使用8080来指定默认访问端口,使用8433指定https访问端口等等,此时就可以通过创建多个Connector来解 ...
- ng指令之 ng-repeat 篇
1>数据绑定 ng-repeat可以绑定数组和JSON对象数据.从下图可以看出控制器的scope()函数得到的对象与controller('ctrlName',['$scope',fun ...
- jQuery 学习之路(1):引子
一.主流 javascript 库 除 jQuery 外,还有 Prototype.Dojo.YUI.ExtJS.MooTools ,其中 Prototype 较老,结构设计较为松散,ExtJS 界面 ...
- CSS hack 汇总
1, IE条件注释法,微软官方推荐的hack方式. <!]> IE6以及IE6以上版本可识别 <![endif]--> <!]> 仅IE7可识别 <![end ...
- R语言 推荐算法 recommenderlab包
recommend li_volleyball 2016年3月20日 library(recommenderlab) library(ggplot2) # data(MovieLense) dim(M ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...