C#: MongoDBでのCRUD操作

C#でMongoDBのORマッパ
 前回に、C#でMongoDBを使うためのORマッパとなるクラスを書いた。今回はそのクラスを使ったり使わなかったりでデータのCRUD操作。
public class Entry

{
[BsonId]
[BsonElement("publishDate")]
[BsonRepresentation(BsonType.Int32)]
public int? EntryID { get; set; } = null;

[DisplayName("タイトル")]
[BsonRepresentation(BsonType.String)]
[BsonElement("title")]
[BsonRequired]
public string Title { get; set; }

[DisplayName("発行日時")]
[BsonRepresentation(BsonType.DateTime)]
[BsonElement("publishDate")]
[BsonRequired]
public DateTime PublishDate { get; set; }

[DisplayName("タグ")]
[BsonRepresentation(BsonType.String)]
[BsonElement("tag")]
public List<string> Tag { get; set; }

[DisplayName("本文")]
[BsonRepresentation(BsonType.String)]
[BsonRequired]
[BsonElement("body")]
public string Body { get; set; }
}



・Create
public bool Insert ()

{
var collection = DbConnection.db.GetCollection<Entry>("entry");
collection.InsertOne(this);

return true;
}



・Read
-IDで絞って読む
public static Entry GetEntry(int id, bool admin=false)

{
var collection = DbConnection.db.GetCollection<Entry>("entry");

FilterDefinition<Entry> filter;
var f = Builders<Entry>.Filter;
filter = f.Eq(e => e.EntryID, id);

var entry = collection.Find<Entry>(filter).FirstOrDefault();

return entry;
}


-日付の新しい順で任意件数読む。スキップもできるように
public static List<Entry> GetRecentEntry(int skip_n, bool admin=false, bool isSitemap=false) {

var collection = DbConnection.db.GetCollection<Entry>("entry");
var skip = skip_n * LIMIT;

FilterDefinition<Entry> filter;
filter = new BsonDocument { };

var sortDoc = new BsonDocument
{
{ "publishDate", -1 },
};

var entries = collection.Find<Entry>(filter).Sort(sortDoc).Limit(LIMIT).Skip(skip).ToList();

return entries;
}


-IDが違って、同じタグのあるEntryを読み込む
public static List<Entry> GetSameTagEntry(List<string> filterTag, int skip_n, int? excludeId = null)

{
var collection = DbConnection.db.GetCollection<Entry>("entry");
var skip = skip_n * LIMIT;

FilterDefinition<Entry> filter;
var f = Builders<Entry>.Filter;
filter = f.Ne(e => e.EntryID, excludeId) &
f.AnyIn(e => e.Tag, filterTag);

var sortDoc = new BsonDocument
{
{ "publishDate", -1 },
};
var entries = collection.Find<Entry>(filter).Sort(sortDoc).Limit(LIMIT).Skip(skip).ToList();

return entries;
}


-任意の月に書かれたEntryを読む
public static List<Entry> FilterByMonth (int year, int month, int skip=0)

{
var collection = DbConnection.db.GetCollection<Entry>("entry");

var dateMin = new DateTime(year, month, 1);
var dateMax = dateMin.AddMonths(1);

FilterDefinition<Entry> filter;
var f = Builders<Entry>.Filter;
filter = f.Gte(e => e.PublishDate, dateMin) &
f.Lt(e => e.PublishDate, dateMax);

var sortDoc = new BsonDocument
{
{ "publishDate", -1 },
};
var entries = collection.Find<Entry>(filter).Sort(sortDoc).Limit(LIMIT).Skip(LIMIT * skip).ToList();

return entries;
}




・Update
public bool Update ()

{
var collection = DbConnection.db.GetCollection<BsonDocument>("entry");

var filter = Builders<BsonDocument>.Filter.Eq("_id", EntryID);
collection.ReplaceOne(filter, this.ToBsonDocument());

return true;
}



・Remove(複数まとめてできるように)
public static bool DeleteMany(List<int> ids)

{
var collection = DbConnection.db.GetCollection<BsonDocument>("entry");

var filter = Builders<BsonDocument>.Filter.In("_id", ids);
collection.DeleteMany(filter);

return true;
}
comment: 0