C# MongoDB Driver 2.0 将“严格模式”Json 转换为“mongo shell 模式”BsonDocument

Jea*_*rie 5 c# serialization mongodb

我有一个“严格模式”Json 字符串,其中日期以“$date”为前缀

"DateCreated\":{\"$date\":1444835457460}
Run Code Online (Sandbox Code Playgroud)

我现在需要“mongo shell 模式”中的字符串,其中

"DateCreated" : ISODate("2015-10-14T15:10:57.460+0000")
Run Code Online (Sandbox Code Playgroud)

为了实现我使用的“严格模式”

.ToJson((new JsonWriterSettings { OutputMode = JsonOutputMode.Strict })
Run Code Online (Sandbox Code Playgroud)

要转换回“mongo shell 模式”,我尝试

.ToJson((new JsonWriterSettings { OutputMode = JsonOutputMode.Shell})
Run Code Online (Sandbox Code Playgroud)

不走运,字符串未转换并且 '$date' 仍然存在(我期待 ISODate ......)

任何的想法 ?

GWi*_*Wam 0

我不确定这是否能回答您的问题,但对于像我一样通过 Google 偶然发现这个问题的人,您可能会发现以下内容很有用。

BsonDocument.Parse从 C# MongoDB.Driver 包中使用:

//var str: '{ "SomeDate": { "$date": "2010-10-10T10:10:00+00:00" } }'
BsonDocument.Parse(str); // Result: {{ "SomeDate": ISODate("2010-10-10T10:10:00Z) }}
Run Code Online (Sandbox Code Playgroud)

这也适用于 unix 时间戳,例如:{ "$date":1286705400000 }