dotnet core use MangoDB
安装MangoDB
同样我这边再次使用Docker, 方便快捷:
# 拉取镜像
docker pull mongo
# 运行镜像
docker run -d -p 37017:27017 --name mongoapp mongo
具体使用
基本代码使用的演示如下:
class Program
{
static void Main(string[] args)
{
//建立连接
var client = new MongoClient("mongodb://127.0.0.1:37017");
//建立数据库
var database = client.GetDatabase("foo");
//建立collection
var collection = database.GetCollection<BsonDocument>("bar");
//为方便测试, 清除部分历史记录
var df = Builders<BsonDocument>.Filter.Gte("counter", 0);
collection.DeleteMany(df);
//插入单条记录
var document = new BsonDocument
{
{ "name", "MongoDB" },
{ "type", "Database" },
{ "count", 1 },
{ "info", new BsonDocument
{
{ "x", 203 },
{ "y", 102 }
}}
};
collection.InsertOne(document);
//异步插入
//await collection.InsertOneAsync(document);
//插入多条记录
// generate 100 documents with a counter ranging from 0 - 99
var documents = Enumerable.Range(0, 100).Select(i => new BsonDocument("counter", i));
collection.InsertMany(documents);
//异步插入
//await collection.InsertManyAsync(documents);
//计数
var count = collection.Count(new BsonDocument());
Console.WriteLine("Count:{0}", count);
//异步计数
//var count = await collection.CountAsync(new BsonDocument());
//查询
//首条记录
var query1 = collection.Find(new BsonDocument()).FirstOrDefault();
Console.WriteLine("Query1:{0}", query1);
//条件查询
var filter = Builders<BsonDocument>.Filter.Eq("counter", 71);
var query2 = collection.Find(filter).FirstOrDefault();
Console.WriteLine("Query2:{0}", query2);
//全部记录
var query3 = collection.Find(new BsonDocument()).ToList();
Console.WriteLine("Query3:{0}", query3.ToJson());
Console.WriteLine("Hello World!");
}
}
项目源码在Gitee.(不好意思, mongo都拼写错了, 不改了)
更多使用请参考官方文档:http://mongodb.github.io/mongo-csharp-driver/2.2/getting_started/quick_tour/
dotnet core use MangoDB的更多相关文章
- dotNet Core开发环境搭建及简要说明
一.安装 .NET Core SDK 在 Windows 上使用 .NET Core 的最佳途径:使用Visual Studio. 免费下载地址: Visual Studio Community 20 ...
- dotnet core 使用 MongoDB 进行高性能Nosql数据库操作
好久没有写过Blog, 每天看着开源的Java社区流口水, 心里满不是滋味. 终于等到了今年六月份 dotnet core 的正式发布, 看着dotnet 社区也一步一步走向繁荣, 一片蒸蒸日上的大好 ...
- dotnet Core Asp.net 项目搭建
Asp.Net Core 介绍 Asp.Net Core 目前最新版本 1.0.0-preview2-003131 Asp.Net Core官网:https://dotnet.github.io/ A ...
- DotNet Core 介绍
前言 asp.net core rtm 6月底即将发布,自己也想着为社区做点共享,刚好最近不太忙,看到社区的小伙伴们都在为dotnet core的推广而贡献力量,项目中刚好在用rc2版本,就多写些文章 ...
- dotnet core 出现Can not find runtime target for framework '.NETCoreApp,Version=v1.6' 的解决办法
如果你在更新dotnet core新的类库后运行程序提示如下的错误: Can not find runtime target for framework '.NETCoreAPP, Version=v ...
- DotNet Core 1.0 集成 CentOS 开发与运行环境部署
一. DotNet Core 1.0 开发环境部署 操作系统安装 我们使用CentOS 7.2.1511版本. 安装libunwind库 执行:sudo yum install libunwi ...
- ubuntu15.10 或者 16.04 或者 ElementryOS 下使用 Dotnet Core
这里我们不讲安装,缺少libicu52自行安装. 安装完成后使用dotnet restore或者build都会失败,一是报编译的dll不适合当前系统,二是编译到ubuntu16.04文件夹下会产生一些 ...
- 北京时间28号0点以后Scott Hanselman同志台宣布dotnet core 1.0 rtm
今日占住微信号头条的好消息<终于来了!微软.Net Core 1.0下载放出>.本人立马跑到官网http://dot.net看了一下,仍然是.net core 1.0 Preview 1版 ...
- DotNet Core 之旅(一)
1.下载安装 DotNetCore.1.0.0-SDK.Preview2-x64.exe 下载链接:https://www.microsoft.com/net/download ps:如果有vs201 ...
随机推荐
- [golang]A modern, fast and scalable websocket framework with elegant API written in Go
A modern, fast and scalable websocket framework with elegant API written in Go http://bit.ly/neffos- ...
- WSL2(Ubuntu)安装Postgres
原文链接:https://www.xu.ci/2019/12/wsl2ubuntupostgres.html 原文作者:博客园--曲高终和寡 *******************如果你看到这一行,说 ...
- CAT中实现异步请求的调用链查看
CAT简介 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在线访 ...
- gcov—a Test Coverage Program
gcov—a Test Coverage Program https://coverage.readthedocs.io/en/v4.5.x/cmd.html 覆盖率测试
- linux: E: Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
今天在使用ubuntu执行下列指令的时候出现了错误: sudo apt-get update 解决办法: 输入以下命令: sudo rm /var/cache/apt/archives/lock su ...
- 【笔试题】某公司中有N名员工。给定所有员工工资的清单
排列员工工资顺序(C++map解法) 题目描述:某公司中有N名员工.给定所有员工工资的清单,财务人员要按照特定的顺序排列员工的工资.他按照工资的频次降序排列.即给定清单中所有频次较高的工资将在频次较低 ...
- 泡泡一分钟: A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem
张宁 A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem "链接:https ...
- Spring cloud微服务安全实战-4-7重构代码以适应真实环境
现在有了认证服务器,也配置了资源服务器.也根据OAuth协议,基于令牌认证的授权也跑通了.基本的概念也有了简单的理解. 往下深入之前,有几个点,还需要说一下 使用scopes来控制权限,scopes可 ...
- SQL语句精妙集合
1一.基础 2 31.说明:创建数据库 4Create DATABASE database-name 5 62.说明:删除数据库 7drop database dbname 8 93. ...
- 【430】BST and Splay Tree
参考:Hello, Splay! 目录: 1. Binary Search Tree 2. Splay Tree