zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mongodb for Linq(T)操作和Json操作BsonDocument

MongoDBJSONJSON 操作 for LinQ
2023-09-11 14:13:57 时间

using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Core.DBContext
{
public class MongoDBbase<T> where T : class, new()
{
MongoClient client;
IMongoDatabase database;
public IMongoCollection<T> collection;
public MongoDBbase()
{
//var client = new MongoClient("mongodb://host:27017,host2:27017/?replicaSet=rs0");
client = new MongoClient("mongodb://39.96.34.12:27017");
database = client.GetDatabase("test");
Type type = typeof(T);
collection = database.GetCollection<T>(type.Name.ToLower());
}

public void traTest()
{
var session = client.StartSession();
try
{
var database = session.Client.GetDatabase("test");
session.StartTransaction(new TransactionOptions(
readConcern: ReadConcern.Snapshot,
writeConcern: WriteConcern.WMajority));
//IMongoCollection<Userinfo> collection = database.GetCollection<Userinfo>("userinfo");
// 切记,使用事务的时候,记得要给需要事务的指令里面吧当前会话的对象传入进去
// 因为事务的完整性是通过sessionid来做的
//collection.InsertOne(session, daqiao);
//throw new Exception("取消事务");
session.CommitTransaction();
}
catch (Exception ex)
{
//回滚
session.AbortTransaction();
}

}
public void DropDatabase()
{
client.DropDatabase("test");
}
public void InsertOne(T model)
{
collection.InsertOne(model);
}
public void InsertMany(params T[] modes)
{
collection.InsertMany(modes);
}
public IMongoQueryable<T> Select()
{
return collection.AsQueryable<T>();
}
public IMongoQueryable<T> Select(int pageIndex, int pageSize)
{
return collection.AsQueryable<T>().Skip(pageSize * (pageIndex - 1)).Take(pageSize);
}
public IMongoQueryable<T> Select(Expression<Func<T, bool>> predicate, Expression<Func<T, object>> keySelector, int pageIndex, int pageSize)
{
return collection.AsQueryable<T>().Where(predicate).OrderBy(keySelector).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
}
public void UpdateMany(Expression<Func<T, bool>> filter, UpdateDefinition<T> update)
{
collection.UpdateMany(filter, update);
}

public void UpdateOne(Expression<Func<T, bool>> filter, T update)
{
collection.ReplaceOne(filter, update);
}

public void DeleteMany(Expression<Func<T, bool>> filter)
{
collection.DeleteMany(filter);
}


}
}

 

-----------------------------json操作  BsonDocument--------------------------

using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Text;

namespace Zhaoxi.MongodbApp
{
public class JsonOperation
{
public static void Show()
{
var client = new MongoClient("mongodb://192.168.1.14:27017");
var database = client.GetDatabase("test");
var document = BsonDocument.Parse("{ a: 1, b: [{ c: 1 }],c: 'ff'}");
var document1 = BsonDocument.Parse("{ a: 6666}");
database.GetCollection<BsonDocument>("userinfo").InsertOne(document1);

database.GetCollection<BsonDocument>("userinfo").InsertOne(document);
}
}
}