Update mongodb demo

This commit is contained in:
sunkaixuan 2025-06-25 14:33:11 +08:00
parent 80a5c25fb3
commit bf1b72cd64
14 changed files with 64 additions and 16 deletions

View File

@ -1,7 +1,6 @@
using MongoDb.Ado.data;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDbTest.DBHelper;
using System;
using System.Collections.Generic;
using System.Data;

View File

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest.DBHelper
namespace MongoDbTest
{
/// <summary>
/// Helper class for database operations

View File

@ -1,5 +1,4 @@
using MongoDbTest.DBHelper;
using SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;

View File

@ -15,6 +15,7 @@ namespace MongoDbTest
QueryWhere.Init();
QuerySelect.Init();
QueryJson.Init();
QueryLeftJoin.Init();
QueryJsonArray.Init();
Insert.Init();
Update.Init();

View File

@ -11,7 +11,7 @@ namespace MongoDbTest
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
var db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
var id=db.Insertable(new Student() { Age = 1, Name = "11", SchoolId = "111", CreateDateTime = DateTime.Now.AddDays(1) }).ExecuteReturnPkList<string>();

View File

@ -12,7 +12,7 @@ namespace MongoDbTest
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
var db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();

View File

@ -13,7 +13,7 @@ namespace MongoDbTest
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
var db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
db.Storageable(new Student() { Name = "a", SchoolId = "1", CreateDateTime = DateTime.Now })

View File

@ -11,7 +11,7 @@ namespace MongoDbTest
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
var db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
db.Insertable(new Book() { Price = 1, CreateTime = DateTime.Now }).ExecuteCommand();

View File

@ -11,7 +11,7 @@ namespace MongoDbTest
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
var db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
db.Insertable(new Student() { Age = 1, Name = "tom", SchoolId = "a", Book = new List<Book>() { new Book() { CreateTime = DateTime.Now, Price = 21 } } }).ExecuteCommand();

View File

@ -0,0 +1,50 @@
using MongoDB.Bson;
using SqlSugar.MongoDb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest
{
internal class QueryLeftJoin
{
internal static void Init()
{
var db = DbHelper.GetNewDb();
db.DbMaintenance.TruncateTable<School, School>();
// 添加学校数据
var school = new School { Name = "TestSchool" };
var ids=db.Insertable(school).ExecuteReturnPkList<string>();
// 添加学生数据SchoolId 关联学校
var student = new Student { Name = "TestStudent", SchoolId = ids.Last() };
db.Insertable(student).ExecuteCommand();
// 添加学生数据SchoolId 关联学校并且没有学校
var student2 = new Student { Name = "TestStudent", SchoolId =
ObjectId.GenerateNewId().ToString()
};
db.Insertable(student2).ExecuteCommand();
//var list=db.Queryable<Student>()
// .LeftJoin<School>((x, y) => x.SchoolId == y.Id)
// .Where((x,y)=>x.Name =="a"||y.Name=="a")
// .Select(x => new
// {
// id=x.Name
// }).ToList();
}
[SqlSugar.SugarTable("UnitStudent123131")]
public class Student : MongoDbBase
{
public string Name { get; set; }
public string SchoolId { get; set; }
}
[SqlSugar.SugarTable("UnitSchool123131")]
public class School : MongoDbBase
{
public string Name { get; set; }
}
}
}

View File

@ -1,7 +1,6 @@
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using MongoDbTest.DBHelper;
using SqlSugar;
using SqlSugar.MongoDb;
using System;
@ -16,7 +15,7 @@ namespace MongoDbTest
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
var db =DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
var dt = DateTime.Now;

View File

@ -13,7 +13,7 @@ namespace MongoDbTest
{
public static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
var db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.CodeFirst.InitTables<School>();

View File

@ -13,7 +13,7 @@ namespace MongoDbTest
public static void Init()
{
//创建DB
var db = DBHelper.DbHelper.GetNewDb();
var db = DbHelper.GetNewDb();
//初始化数据
InitializeStudentData(db);

View File

@ -11,7 +11,7 @@ namespace MongoDbTest
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
var db = DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
db.Insertable(new Student() { Age = 1, Name = "11", SchoolId = "111", CreateDateTime = DateTime.Now }).ExecuteCommandAsync().GetAwaiter().GetResult();