当前位置:网站首页>C#访问MongoDB并执行CRUD操作
C#访问MongoDB并执行CRUD操作
2022-06-30 08:36:00 【皓月如我】
先在NuGet中下载并安装驱动mongocsharpdriver,和客户端包MongoDB.Driver
代码如下
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace TestMongoDB
{
[Serializable]
class Customer
{
public int id {
set; get; }
public string name {
set; get; }
public int age {
set; get; }
}
class Program
{
public static void Main()
{
MongoClient client;
MongoServer server;
client = new MongoClient("mongodb://admin:[email protected]:27017/admin");
if (client != null)
{
Console.WriteLine("MongoDB connect success.");
server = client.GetServer();
MongoDatabase mdb = server.GetDatabase("foo");
MongoCollection<BsonDocument> collection = mdb.GetCollection("bar");
// Select operation
var records = collection.FindAll();
foreach (BsonDocument record in records)
{
Console.WriteLine(record);
}
// Insert operation
Customer customer = new Customer
{
id = 8,
name = "zahngsan",
age = 28
};
collection.Insert<Customer>(customer);
// Update operation
IMongoQuery iq4u = Query.EQ("name", "zhangsan_1");
IMongoUpdate iu = Update.Set("name", "zhangsan").Set("age", 98);
collection.Update(iq4u, iu, UpdateFlags.Multi);
// Delete operation
IMongoQuery iq4d = Query.EQ("name", "zhangsan_2");
collection.Remove(iq4d);
// Select as Object
/*MongoCollection<Customer> collection2 = mdb.GetCollection<Customer>("bar"); var customer1 = collection2.FindOne(); var customer2 = collection2.FindOneAs<Customer>(); foreach (BsonDocument record in records) { var customer3 = BsonSerializer.Deserialize<Customer>(record); }*/
}
else
{
Console.WriteLine("MongoDB connect fail.");
}
}
}
}
边栏推荐
- End-to-end 3D Point Cloud Instance Segmentation without Detection
- 2021-02-27
- Redis设计与实现(八)| 事务
- Redis design and Implementation (IV) | master-slave replication
- Rendering engine development
- Redis design and Implementation (VI) | cluster (sharding)
- 14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
- 国债逆回购绝对安全吗 网上怎么开户
- mysql基础入门 动力节点[老杜]课堂作业
- [nvme2.0b 14-8] set features (Part 2)
猜你喜欢
Redis design and Implementation (VIII) | transaction
电流探头的干扰源电流谱测试
Redis design and Implementation (IV) | master-slave replication
【NVMe2.0b 14】NVMe Admin Command Set
swagger使用
Cesium learning notes (V) custom geometry and appearance
增强for循环的增删操作 & 迭代器删除集合元素
How can we get a satisfactory salary? These routines still need to be mastered
Redis设计与实现(八)| 事务
Self made GIF dynamic graph -gifcam
随机推荐
codeforces每日5题(均1700)-第三天
[untitled]
Redis设计与实现(四)| 主从复制
Unity简单shader
Map,String,Json之間轉換
mysql基础入门 day4 动力节点[老杜]课堂笔记
Rendering engine development
Axure制作菜单栏效果
基于Svelte3.x桌面端UI组件库Svelte UI
【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
Sword finger offer II 075 Array relative sort (custom sort, count sort)
Dart tips
在浏览器输入url到页面展示出来
1. Problems related to OpenGL window and environment configuration
[untitled]
Redis设计与实现(七)| 发布 & 订阅
[untitled]
Codeworks 5 questions per day (1700 for each) - the third day
2021-05-17
Cesium learning notes (IV) visual image & Terrain