当前位置:网站首页>C accesses mongodb and performs CRUD operations
C accesses mongodb and performs CRUD operations
2022-06-30 08:37:00 【The bright moon is like me】
First in NuGet Download and install the driver mongocsharpdriver, And client packages MongoDB.Driver
The code is as follows
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.");
}
}
}
}
边栏推荐
- CUDA realizes L2 European distance
- C#访问SQL Server数据库两种方式的比较(SqlDataReader vs SqlDataAdapter)
- Conversion between map, string and JSON
- Sword finger offer II 074 Merge interval (sort, array)
- 国债逆回购绝对安全吗 网上怎么开户
- Redis design and Implementation (III) | interaction between server and client (event IO model)
- 基于Svelte3.x桌面端UI组件库Svelte UI
- Axure make menu bar effect
- Gilbert Strang's course notes on linear algebra - Lesson 3
- What are the Amazon evaluation terms?
猜你喜欢

Summary of common pytoch APIs

【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
![[nvme2.0b 14-7] set features (Part 1)](/img/0d/c26ae2475ae69291d83b4096ea942b.png)
[nvme2.0b 14-7] set features (Part 1)

一次cpu 跌底排查

自制GIF动态图-gifcam

Wechat official account third-party platform development, zero foundation entry. I want to teach you

What are the Amazon evaluation terms?

Flink SQL 自定义 Connector

Gilbert Strang's course notes on linear algebra - Lesson 4

从0开始构建一个瀚高数据库Docker镜像
随机推荐
Detectron2 source code reading 3-- encapsulating dataset with mapper
El input limit can only input numbers
Dart tips
Circuit analysis of current probe
Flink SQL 自定义 Connector
Self made GIF dynamic graph -gifcam
Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)
证券开户的优惠怎样才能得到?在线开户安全?
【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
What are the Amazon evaluation terms?
启动jar包报错UnsupportedClassVersionError,如何修复
mysql基础入门 动力节点[老杜]课堂作业
2021-05-17
CUDA implements matrix replication
C# ListBox如何获取选中的内容(搜了很多无效的文章)
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
Build a docker image of Henkel database from 0
Redis设计与实现(五)| Sentinel哨兵
icon资源
Markdown支持的emoji图标