mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-27 19:41:17 +08:00
Update mongodb
This commit is contained in:
parent
e1c68275ef
commit
2244cd40d2
@ -19,6 +19,7 @@ namespace MongoDbTest
|
||||
Insert.Init();
|
||||
Update.Init();
|
||||
Delete.Init();
|
||||
InsertOrUpdate.Init();
|
||||
}
|
||||
public static void ThrowUnitError()
|
||||
{
|
||||
|
33
Src/Asp.NetCore2/MongoDbTest/UnitTest/InsertOrUpdate.cs
Normal file
33
Src/Asp.NetCore2/MongoDbTest/UnitTest/InsertOrUpdate.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using MongoDb.Ado.data;
|
||||
using MongoDB.Driver;
|
||||
using SqlSugar.MongoDb;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MongoDbTest
|
||||
{
|
||||
internal class InsertOrUpdate
|
||||
{
|
||||
internal static void Init()
|
||||
{
|
||||
var db = DBHelper.DbHelper.GetNewDb();
|
||||
db.CodeFirst.InitTables<Student>();
|
||||
db.Storageable(new Student() { Name = "a", SchoolId = "1", CreateDateTime = DateTime.Now })
|
||||
.ExecuteCommand();
|
||||
}
|
||||
[SqlSugar.SugarTable("UnitStudent1zzsds3z1")]
|
||||
public class Student : MongoDbBase
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public string SchoolId { get; set; }
|
||||
|
||||
public int Age { get; set; }
|
||||
|
||||
public DateTime CreateDateTime { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using Dm.util;
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -8,6 +10,68 @@ namespace SqlSugar.MongoDb
|
||||
{
|
||||
public class MongoDbQueryable<T> : QueryableProvider<T>
|
||||
{
|
||||
public override ISugarQueryable<T> WhereClassByPrimaryKey(List<T> list)
|
||||
{
|
||||
var filterDoc = new BsonDocument();
|
||||
|
||||
if (list.HasValue())
|
||||
{
|
||||
var columns = this.Context.EntityMaintenance.GetEntityInfo<T>()
|
||||
.Columns.Where(it => it.IsIgnore == false && it.IsPrimarykey == true).ToList();
|
||||
|
||||
Check.Exception(columns == null || columns.Count == 0, "{0} no primary key, Can not use WhereClassByPrimaryKey ", typeof(T).Name);
|
||||
Check.Exception(this.QueryBuilder.IsSingle() == false, "No support join query");
|
||||
|
||||
var orArray = new BsonArray();
|
||||
|
||||
foreach (var item in list)
|
||||
{
|
||||
var andDoc = new BsonDocument();
|
||||
|
||||
foreach (var column in columns)
|
||||
{
|
||||
var value = column.PropertyInfo.GetValue(item, null);
|
||||
BsonValue bsonValue;
|
||||
|
||||
if (value is Enum && this.Context.CurrentConnectionConfig?.MoreSettings?.TableEnumIsString != true)
|
||||
{
|
||||
bsonValue = new BsonInt64(Convert.ToInt64(value));
|
||||
}
|
||||
else if (value != null && column.UnderType == UtilConstants.DateType)
|
||||
{
|
||||
bsonValue = new BsonDateTime(Convert.ToDateTime(value));
|
||||
}
|
||||
else
|
||||
{
|
||||
bsonValue = BsonValue.Create(value);
|
||||
}
|
||||
|
||||
andDoc[column.DbColumnName] = bsonValue;
|
||||
}
|
||||
|
||||
if (andDoc.ElementCount > 0)
|
||||
{
|
||||
orArray.Add(andDoc);
|
||||
}
|
||||
}
|
||||
|
||||
if (orArray.Count == 1)
|
||||
{
|
||||
filterDoc = orArray[0].AsBsonDocument;
|
||||
}
|
||||
else
|
||||
{
|
||||
filterDoc = new BsonDocument("$or", orArray);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 等价于 WHERE 1=2
|
||||
filterDoc = new BsonDocument("$expr", new BsonDocument("$eq", new BsonArray { 1, 2 }));
|
||||
}
|
||||
this.QueryBuilder.WhereInfos.Add(filterDoc.ToJson(UtilMethods.GetJsonWriterSettings()));
|
||||
return this;
|
||||
}
|
||||
public override int Count()
|
||||
{
|
||||
return GetCount(); ;
|
||||
|
Loading…
Reference in New Issue
Block a user