SqlSugar/Src/Asp.NetCore2/MongoDbTest/AdoTest.cs
2025-04-26 19:34:30 +08:00

46 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using MongoDb.Ado.data;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest
{
public class AdoTest
{
public static void Init()
{
MongoClientTest();
MongoDbConnectionTest();
}
private static void MongoDbConnectionTest()
{
var db= new MongoDbConnection(DbHelper.SqlSugarConnectionString);
var database= db.GetDatabase();
var collections = database.GetCollection<BsonDocument>("b");
// 插入一个文档MongoDB 会创建数据库和集合
var document = new BsonDocument { { "name", "bbbbbb" }, { "age", 30 } };
collections.InsertOne(document);
var list = collections.AsQueryable<BsonDocument>().ToList();
}
private static void MongoClientTest()
{
//开发中
var client = new MongoClient(DbHelper.ConnectionString);
var database = client.GetDatabase("SqlSugarDb");
// 获取当前数据库中的所有集合
var collections = database.GetCollection<BsonDocument>("a");
// 插入一个文档MongoDB 会创建数据库和集合
var document = new BsonDocument { { "name", "aaaa" }, { "age", 30 } };
collections.InsertOne(document);
var list = collections.AsQueryable<BsonDocument>().ToList();
}
}
}