BuildFeed/RedisMongoMigration/Mongo/MongoRole.cs

60 lines
1.4 KiB
C#
Raw Normal View History

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
2015-09-12 22:23:13 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RedisMongoMigration.Mongo
{
public class RoleModel
2015-09-12 22:23:13 +08:00
{
[BsonId]
public Guid Id { get; set; }
public string RoleName { get; set; }
public Guid[] Users { get; set; }
}
public class MongoRole
{
private const string _buildCollectionName = "roles";
private MongoClient _dbClient;
private IMongoCollection<RoleModel> _buildCollection;
public MongoRole()
{
_dbClient = new MongoClient(new MongoClientSettings()
{
Server = new MongoServerAddress("localhost", 27017)
});
_buildCollection = _dbClient.GetDatabase("BuildFeed").GetCollection<RoleModel>(_buildCollectionName);
}
public List<RoleModel> Select()
{
var task = _buildCollection.Find(new BsonDocument()).ToListAsync();
task.Wait();
return task.Result;
}
public void Insert(RoleModel item)
{
item.Id = Guid.NewGuid();
var task = _buildCollection.InsertOneAsync(item);
task.Wait();
}
public void InsertAll(IEnumerable<RoleModel> items)
{
var task = _buildCollection.InsertManyAsync(items);
task.Wait();
}
}
2015-09-12 22:23:13 +08:00
}