mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
Update mongodb
This commit is contained in:
parent
8870fb173c
commit
155110908d
@ -14,6 +14,7 @@ namespace MongoDbTest
|
|||||||
QuerySingle.Init();
|
QuerySingle.Init();
|
||||||
QueryWhere.Init();
|
QueryWhere.Init();
|
||||||
QuerySelect.Init();
|
QuerySelect.Init();
|
||||||
|
QueryJson.Init();
|
||||||
Insert.Init();
|
Insert.Init();
|
||||||
Update.Init();
|
Update.Init();
|
||||||
Delete.Init();
|
Delete.Init();
|
||||||
|
48
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson.cs
Normal file
48
Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryJson.cs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
using SqlSugar.MongoDb;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace MongoDbTest
|
||||||
|
{
|
||||||
|
public class QueryJson
|
||||||
|
{
|
||||||
|
internal static void Init()
|
||||||
|
{
|
||||||
|
var db = DBHelper.DbHelper.GetNewDb();
|
||||||
|
db.CodeFirst.InitTables<Student>();
|
||||||
|
db.DbMaintenance.TruncateTable<Student>();
|
||||||
|
db.Insertable(new Book() { Price = 1, CreateTime = DateTime.Now }).ExecuteCommand();
|
||||||
|
var data1=db.Queryable<Book>().Where(it => it.Price == 1).ToList();
|
||||||
|
db.Insertable(new Student() { Age = 1, Name = "tom", SchoolId = "a", Book = new Book() { CreateTime = DateTime.Now, Price = 1 } }).ExecuteCommand();
|
||||||
|
db.Insertable(new Student() { Age = 1, Name = "tom2", SchoolId = "a2", Book = new Book() { CreateTime = DateTime.Now, Price = 2 } }).ExecuteCommand();
|
||||||
|
var data2 = db.Queryable<Student>().Where(it => it.Book.Price == 1).ToList();
|
||||||
|
if (data2.Count != 1) Cases.ThrowUnitError();
|
||||||
|
if (data2.First().Book.Price != 1) Cases.ThrowUnitError();
|
||||||
|
}
|
||||||
|
|
||||||
|
[SqlSugar.SugarTable("UnitStudentdfsds3zzz1")]
|
||||||
|
public class Student : MongoDbBase
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public string SchoolId { get; set; }
|
||||||
|
|
||||||
|
public int Age { get; set; }
|
||||||
|
|
||||||
|
public DateTime CreateDateTime { get; set; }
|
||||||
|
|
||||||
|
[SqlSugar.SugarColumn(IsJson = true)]
|
||||||
|
public Book Book { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Book
|
||||||
|
{
|
||||||
|
public decimal Price { get; set; }
|
||||||
|
public DateTime CreateTime { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -43,10 +43,7 @@ namespace SqlSugar.MongoDb
|
|||||||
{ "_id", new BsonDocument { { "$in", bsonArray } } }
|
{ "_id", new BsonDocument { { "$in", bsonArray } } }
|
||||||
};
|
};
|
||||||
|
|
||||||
string json = filter.ToJson(new MongoDB.Bson.IO.JsonWriterSettings
|
string json = filter.ToJson(UtilMethods.GetJsonWriterSettings()); // 使用 MongoDB 驱动的序列化
|
||||||
{
|
|
||||||
OutputMode = MongoDB.Bson.IO.JsonOutputMode.Shell
|
|
||||||
}); // 使用 MongoDB 驱动的序列化
|
|
||||||
jsonObjects.Add(json);
|
jsonObjects.Add(json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,7 @@ namespace SqlSugar.MongoDb
|
|||||||
context.context = this.SugarContext.Context;
|
context.context = this.SugarContext.Context;
|
||||||
context.queryBuilder = this.SugarContext.QueryBuilder;
|
context.queryBuilder = this.SugarContext.QueryBuilder;
|
||||||
var sql=MongoNestedTranslator.Translate(expression, context);
|
var sql=MongoNestedTranslator.Translate(expression, context);
|
||||||
var shellString = sql.ToJson(new JsonWriterSettings
|
var shellString = sql.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
{
|
|
||||||
OutputMode = JsonOutputMode.Shell
|
|
||||||
});
|
|
||||||
this.Result.Append(shellString);
|
this.Result.Append(shellString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
|
using MongoDB.Bson.IO;
|
||||||
|
using MongoDB.Bson.Serialization;
|
||||||
|
using NetTaste;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
@ -10,6 +14,49 @@ namespace SqlSugar.MongoDb
|
|||||||
{
|
{
|
||||||
public class MongoDbInsertBuilder : InsertBuilder
|
public class MongoDbInsertBuilder : InsertBuilder
|
||||||
{
|
{
|
||||||
|
public MongoDbInsertBuilder()
|
||||||
|
{
|
||||||
|
this.SerializeObjectFunc = it =>
|
||||||
|
{
|
||||||
|
object value =it;
|
||||||
|
|
||||||
|
if (value is IEnumerable enumerable)
|
||||||
|
{
|
||||||
|
var realType = value.GetType();
|
||||||
|
var bson = value.ToBson(realType);
|
||||||
|
var json = bson.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var realType = it.GetType();
|
||||||
|
var bson = it.ToBson(realType); // → byte[]
|
||||||
|
var doc = BsonSerializer.Deserialize<BsonDocument>(bson); // → BsonDocument
|
||||||
|
var json = doc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.DeserializeObjectFunc = (json, type) =>
|
||||||
|
{
|
||||||
|
if (json is Dictionary<string, object> keyValues)
|
||||||
|
{
|
||||||
|
// 先用 Dictionary 构建 BsonDocument
|
||||||
|
var bsonDoc = new BsonDocument();
|
||||||
|
|
||||||
|
foreach (var kvp in keyValues)
|
||||||
|
{
|
||||||
|
bsonDoc.Add(kvp.Key, BsonValue.Create(kvp.Value));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 再用 BsonSerializer 反序列化为 T
|
||||||
|
return BsonSerializer.Deserialize(bsonDoc, type);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
public override string SqlTemplate
|
public override string SqlTemplate
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -59,14 +106,18 @@ namespace SqlSugar.MongoDb
|
|||||||
foreach (var col in group)
|
foreach (var col in group)
|
||||||
{
|
{
|
||||||
// 自动推断类型,如 string、int、bool、DateTime、ObjectId 等
|
// 自动推断类型,如 string、int、bool、DateTime、ObjectId 等
|
||||||
|
if (col.IsJson == true)
|
||||||
|
{
|
||||||
|
doc[col.DbColumnName] = BsonDocument.Parse(col.Value?.ToString());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
doc[col.DbColumnName] = UtilMethods.MyCreate(col.Value);
|
doc[col.DbColumnName] = UtilMethods.MyCreate(col.Value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 转为 JSON 字符串(标准 MongoDB shell 格式)
|
// 转为 JSON 字符串(标准 MongoDB shell 格式)
|
||||||
string json = doc.ToJson(new MongoDB.Bson.IO.JsonWriterSettings
|
string json = doc.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
{
|
|
||||||
OutputMode = MongoDB.Bson.IO.JsonOutputMode.Shell // 可改成 Strict
|
|
||||||
});
|
|
||||||
|
|
||||||
jsonObjects.Add(json);
|
jsonObjects.Add(json);
|
||||||
}
|
}
|
||||||
|
@ -137,10 +137,7 @@ namespace SqlSugar.MongoDb
|
|||||||
{ "update", update }
|
{ "update", update }
|
||||||
};
|
};
|
||||||
|
|
||||||
string json = entry.ToJson(new MongoDB.Bson.IO.JsonWriterSettings
|
string json = entry.ToJson(UtilMethods.GetJsonWriterSettings());
|
||||||
{
|
|
||||||
OutputMode = MongoDB.Bson.IO.JsonOutputMode.Shell // JSON标准格式,带双引号
|
|
||||||
});
|
|
||||||
|
|
||||||
operations.Add(json);
|
operations.Add(json);
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,7 @@ namespace SqlSugar.MongoDb
|
|||||||
}
|
}
|
||||||
internal static MongoDB.Bson.IO.JsonWriterSettings GetJsonWriterSettings()
|
internal static MongoDB.Bson.IO.JsonWriterSettings GetJsonWriterSettings()
|
||||||
{
|
{
|
||||||
return new MongoDB.Bson.IO.JsonWriterSettings
|
return new MongoDB.Bson.IO.JsonWriterSettings { };
|
||||||
{
|
|
||||||
OutputMode = MongoDB.Bson.IO.JsonOutputMode.Shell // JSON标准格式,带双引号
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
internal static DateTime GetMinDate(ConnectionConfig currentConnectionConfig)
|
internal static DateTime GetMinDate(ConnectionConfig currentConnectionConfig)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user