当前位置:网站首页>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.");
}
}
}
}
边栏推荐
- Flink 数据偶尔数据积压导致checkpoint失败
- codeforces每日5题(均1700)-第三天
- PHP API to obtain QR code and combine to generate pictures
- Swagger use
- Axure make menu bar effect
- Does the oscilloscope probe affect the measurement of capacitive load?
- c#获取当前的时间戳
- 电流探头电路分析
- Gilbert Strang's course notes on linear algebra - Lesson 1
- php api获取二维码、组合生成图片
猜你喜欢

Summary of common pytoch APIs

Flink SQL 自定义 Connector

【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command

Redis设计与实现(八)| 事务

1. Problems related to OpenGL window and environment configuration

Does the oscilloscope probe affect the measurement of capacitive load?

Interference source current spectrum test of current probe

Detectron2 source code reading 3-- encapsulating dataset with mapper

Codeworks 5 questions per day (1700 for each) - the third day

Circuit analysis of current probe
随机推荐
【NVMe2.0b 14-4】Directive Send/Receive command
Introduction to MySQL basics day4 power node [Lao Du] class notes
Graffiti Wi Fi & ble SoC development slide strip
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
layer. Open processing method when the passed value is an array or the value is too long
Redis design and Implementation (VII) | publish & subscribe
A troubleshooting of CPU bottom falling
Summary of common pytoch APIs
Redis设计与实现(八)| 事务
Is the reverse repurchase of treasury bonds absolutely safe? How to open an account online
This point in JS
Map,String,Json之間轉換
File upload component on success event, add custom parameters
Dart tips
【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
Gilbert Strang's course notes on linear algebra - Lesson 4
【NVMe2.0b 14-5】Firmware Download/Commit command
Game 280 problem2: minimum operands to turn an array into an alternating array
Redis设计与实现(三)| 服务器与客户端的交互(事件IO模型)
Sword finger offer II 075 Array relative sort (custom sort, count sort)