当前位置:网站首页>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.");
}
}
}
}
边栏推荐
- TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
- Deploy the cow like customer network project on the ECS
- Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
- Cesium learning notes (I)
- Cesium learning notes (II) uploading data using rest API
- 【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
- Redis design and Implementation (VII) | publish & subscribe
- codeforces每日5题(均1700)-第三天
- Axure make menu bar effect
- 国债逆回购绝对安全吗 网上怎么开户
猜你喜欢
Redis design and Implementation (II) | database (deletion strategy & expiration elimination strategy)
【NVMe2.0b 14】NVMe Admin Command Set
vim 从嫌弃到依赖(21)——跨文件搜索
Environment configuration of ROS Aubo manipulator
Using typera+picgo to realize automatic uploading of markdown document pictures
Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
Gilbert Strang's course notes on linear algebra - Lesson 3
Redis设计与实现(二)| 数据库(删除策略&过期淘汰策略)
Redis设计与实现(六)| 集群(分片)
Gilbert Strang's course notes on linear algebra - Lesson 1
随机推荐
Cesium learning notes (II) uploading data using rest API
Oracle expansion table space installed in docker
Redis design and Implementation (IV) | master-slave replication
Flink Exception -- No ExecutorFactory found to execute the application
CUDA realizes L2 European distance
Sword finger offer II 074 Merge interval (sort, array)
Map,String,Json之間轉換
【NVMe2.0b 14】NVMe Admin Command Set
Deploy the cow like customer network project on the ECS
云服务器上部署仿牛客网项目
2021-02-18
【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
JS中的this指向
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
Interference source current spectrum test of current probe
Enter the URL in the browser and display it on the page
Cesium learning notes (III) creating instances
Game 280 problem2: minimum operands to turn an array into an alternating array
从0开始构建一个瀚高数据库Docker镜像
Enhance the add / delete operation of for loop & iterator delete collection elements