[英]ObjectId.getTimestamp() from MongoDB .NET Driver
我正在使用MongoDB .NET驅動程序的v2.9,並想使用ObjectId.getTimestamp() Shell方法。 有什么辦法可以從我的C#代碼中調用它嗎?
我已經搜索過Mongo SDK文檔,但沒有找到我可以使用的任何東西。
這是我與Microsoft docs教程一起使用的示例模型
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace WebApi.Models
{
public class Currency
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
// Need a CreatedAt getter that uses ObjectId.getTimestamp()
public string Name { get; set; }
public string IsoNumeric { get; set; }
public int MinorUnit { get; set; }
public string Symbol { get; set; }
public string SubUnit { get; set; }
}
}
在我的IDE中自動填充了一些隨機關鍵字之后,我找到了ObjectId類,並且它具有CreationTime獲取器-http: //mongodb.github.io/mongo-csharp-driver/2.9/apidocs/html/P_MongoDB_Bson_ObjectId_CreationTime.htm
在我的課堂上,我添加了以下內容,它似乎可以根據我的需要正常工作
[BsonIgnore]
public DateTime? CreatedAt => Id != null ? new ObjectId(Id).CreationTime : (DateTime?) null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.