当前位置:网站首页>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.");
}
}
}
}
边栏推荐
猜你喜欢
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
【NVMe2.0b 14-8】Set Features(下篇)
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
微信公众号第三方平台开发,零基础入门。想学我教你啊
VIM from dislike to dependence (21) -- cross file search
云服务器上部署仿牛客网项目
Flink Exception -- No ExecutorFactory found to execute the application
我们如何拿到自己满意的薪资呢?这些套路还是需要掌握的
Sword finger offer II 074 Merge interval (sort, array)
Detailed explanation of pytoch's scatter function
随机推荐
【NVMe2.0b 14-4】Directive Send/Receive command
Build a docker image of Henkel database from 0
【NVMe2.0b 14-2】Create/Delete Queue
Cesium learning notes (VI) particle system
Using typera+picgo to realize automatic uploading of markdown document pictures
Summary of common pytoch APIs
2021-02-27
C # listbox how to get the selected content (search many invalid articles)
Qt连接神通数据库
Map,String,Json之間轉換
C#访问SQL Server数据库两种方式的比较(SqlDataReader vs SqlDataAdapter)
Redis设计与实现(六)| 集群(分片)
Introduction to MySQL basics day4 power node [Lao Du] class notes
Cesium learning notes (I)
Wechat official account third-party platform development, zero foundation entry. I want to teach you
示波器探头对测量电容负荷有影响吗?
[data analysis and display]
Enter the URL in the browser and display it on the page
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
File upload component on success event, add custom parameters