2016-04-30 1 views
0

MongoDBと一緒に作業していて、少なくとも私にとっては奇妙な動作が見つかりました。私はC#から挿入してMongoDBから検索する時に時差があります。Cから挿入した後のmongodbからのタイムスタンプの取得が異なります

マイエンティティ:

public bool Insert(AccountCategories _input) 
{ 
    _input = new AccountCategories(); 
    _input.CreatedBy = "super-admin"; 
    _input.CreatedTime = DateTime.Now; 
    _input.Id = new ObjectId(); 
    _input.IsActive = true; 
    _input.Name = "test-name"; 

    var _result = _repo.Insert(_input); 

    return _result; 
} 
  • 挿入されたデータ:{4/30/16 9時04分36秒PM}
  • 検索

    [BsonId] 
    public ObjectId Id { get; set; } 
    public bool IsActive { get; set; } 
    public string CreatedBy { get; set; } 
    public DateTime CreatedTime { get; set; } 
    public string Name { get; set; } 
    

    タイムスタンプをコード下に使用して挿入しました。データ:{4/30/16 2:04:36 PM}

Bson属性を追加してエンティティを変更しようとしましたが、機能していませんでした。

[BsonRepresentation(BsonType.Document)] 
public DateTime CreatedTime { get; set; } 

なぜこの現象が発生したのですか?どうすればこの問題を解決できますか?

答えて

0

このサイトとgoogleでの検索中にキーワードを変更した後、回答が見つかりました。

のMongoDBマニュアルによると: は時間がUTCにデフォルト設定されてhttps://docs.mongodb.org/manual/tutorial/model-time-data/

、それは私が7時間差を得た理由です(私のマニュアル最初に見ていないことが悪い) は、だから私はBsonAttributeを追加することによって、私の問題を解決するために管理

[BsonDateTimeOptions(Kind=DateTimeKind.Local)] 
public DateTime CreatedTime { get; set; } 

源:Dealing with how MongoDB stores DateTime when used with Service Locator Pattern

日時に以下のように
関連する問題