当前位置:网站首页>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.");
}
}
}
}
边栏推荐
- 我们如何拿到自己满意的薪资呢?这些套路还是需要掌握的
- Source code interpretation of detectron2 1--engine
- C# ListBox如何获取选中的内容(搜了很多无效的文章)
- swagger使用
- 微信公众号第三方平台开发,零基础入门。想学我教你啊
- 维基媒体基金会公布新商业产品“维基媒体企业”首批客户
- [untitled]
- 基于Svelte3.x桌面端UI组件库Svelte UI
- Enhance the add / delete operation of for loop & iterator delete collection elements
- Axure制作菜单栏效果
猜你喜欢

Wikimedia Foundation announces the first customers of its new commercial product "Wikimedia enterprise"

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

Redis design and Implementation (VIII) | transaction

Redis设计与实现(三)| 服务器与客户端的交互(事件IO模型)

Flink 数据偶尔数据积压导致checkpoint失败

Redis设计与实现(五)| Sentinel哨兵

【NVMe2.0b 14-5】Firmware Download/Commit command

Detectron2 source code reading 3-- encapsulating dataset with mapper

【NVMe2.0b 14】NVMe Admin Command Set

Redis设计与实现(七)| 发布 & 订阅
随机推荐
VIM from dislike to dependence (21) -- cross file search
Redis设计与实现(七)| 发布 & 订阅
[untitled]
2021-05-17
Sword finger offer II 074 Merge interval (sort, array)
codeforces每日5题(均1700)-第三天
Qt通过Url下载文件
Experiment 6 examination
vite项目require语法兼容问题解决require is not defined
CUDA implements matrix replication
自制GIF动态图-gifcam
[untitled]
Redis design and Implementation (V) | sentinel sentry
从0开始构建一个瀚高数据库Docker镜像
【NVMe2.0b 14-5】Firmware Download/Commit command
Interference source current spectrum test of current probe
Unity简单shader
Build a docker image of Henkel database from 0
Detectron2 source code reading 2--- using the configurable decorator to build the dataloader
Experiment 4 QT