当前位置:网站首页>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.");
}
}
}
}
边栏推荐
- Gilbert Strang's course notes on linear algebra - Lesson 2
- Cesium learning notes (I)
- Sword finger offer II 075 Array relative sort (custom sort, count sort)
- Be careful of this hole in transmittable thread local
- [kotlin collaboration process] complete the advanced kotlin collaboration process
- Redis设计与实现(六)| 集群(分片)
- What are the Amazon evaluation terms?
- JS中的this指向
- Qqquickpainteditem implements graffiti program drawing board
- [nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel
猜你喜欢

Detectron2 source code reading 3-- encapsulating dataset with mapper

【NVMe2.0b 14-2】Create/Delete Queue
![[untitled]](/img/b8/e3f54fe5d1079663799887e62cb07c.jpg)
[untitled]

Deploy the cow like customer network project on the ECS

Sword finger offer II 075 Array relative sort (custom sort, count sort)

云服务器上部署仿牛客网项目

【NVMe2.0b 14-2】Create/Delete Queue

涂鸦Wi-Fi&BLE SoC开发幻彩灯带

Summary of common pytoch APIs
![[data analysis and display]](/img/86/19260ee664769c98588d8b0783ef28.jpg)
[data analysis and display]
随机推荐
mysql基础入门 day3 动力节点[老杜]课堂笔记
Redis设计与实现(三)| 服务器与客户端的交互(事件IO模型)
Does the oscilloscope probe affect the measurement of capacitive load?
el-input 限制只能输数字
mysql基础入门 动力节点[老杜]课堂作业
【NVMe2.0b 14】NVMe Admin Command Set
Enter the URL in the browser and display it on the page
Gilbert Strang's course notes on linear algebra - Lesson 2
How can we get a satisfactory salary? These routines still need to be mastered
关于Lombok的@Data注解
Redis design and Implementation (IV) | master-slave replication
Cesium learning notes (III) creating instances
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
Understanding society at the age of 14 - reading notes on "happiness at work"
Using typera+picgo to realize automatic uploading of markdown document pictures
icon资源
Pytorch BERT
File upload component on success event, add custom parameters
Gilbert Strang's course notes on linear algebra - Lesson 3
php api获取二维码、组合生成图片