MongoDB via Dotnet Core数据映射详解
<div id="output_wrapper_id" class="output_wrapper" style="font-size: 16px; color: rgba(62, 62, 62, 1); line-height: 1.6; word-spacing: 0; letter-spacing: 0; font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif"><blockquote style="line-height: inherit; display: block; padding: 15px 15px 15px 1rem; font-size: 0.9em; margin: 1em 0; color: rgba(129, 145, 152, 1); border-left: 6px solid rgba(220, 230, 240, 1); background-color: rgba(242, 247, 251, 1); overflow: auto; word-wrap: normal; word-break: normal">
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 0">用好数据映射,MongoDB via Dotnet Core开发变会成一件超级快乐的事。</p>
</blockquote>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<h1 id="h" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">一、前言</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">MongoDB这几年已经成为NoSQL的头部数据库。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">由于MongoDB <strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold">free schema</strong>的特性,使得它在互联网应用方面优于常规数据库,成为了相当一部分大厂的主数据选择;而它的<strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold">快速布署和开发简单</strong>的特点,也吸引着大量小开发团队的支持。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic">关于MongoDB快速布署,我在15分钟从零开始搭建支持10w+用户的生产环境(二)里有写,需要了可以去看看。</em></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">作为一个数据库,基本的操作就是CRUD。MongoDB的CRUD,不使用SQL来写,而是提供了更简单的方式。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">方式一、BsonDocument方式</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">BsonDocument方式,适合能熟练使用MongoDB Shell的开发者。MongoDB Driver提供了完全覆盖Shell命令的各种方式,来处理用户的CRUD操作。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这种方法自由度很高,可以在不需要知道完整数据集结构的情况下,完成数据库的CRUD操作。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">方式二、数据映射方式</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">数据映射是最常用的一种方式。准备好需要处理的数据类,直接把数据类映射到MongoDB,并对数据集进行CRUD操作。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">下面,对数据映射的各个部分,我会逐个说明。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic"> <span style="font-size: small; color: inherit; line-height: inherit; margin: 0; padding: 0">为了防止不提供原网址的转载,特在这里加上原文链接:https://www.cnblogs.com/tiger-wang/p/13185605.html</span></em></p>
<h1 id="h-1" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">二、开发环境&基础工程</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个Demo的开发环境是:Mac + VS Code + Dotnet Core 3.1.2。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">建立工程:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">% dotnet new sln -o demo<br>The template <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Solution File"</span> was created successfully.<br>% <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">cd</span> demo <br>% dotnet new console -o demo<br>The template <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Console Application"</span> was created successfully.<br><br>Processing post-creation actions...<br>Running <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">'dotnet restore'</span> on demo/demo.csproj...<br> Determining projects to restore...<br> Restored demo/demo/demo.csproj (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">in</span> 162 ms).<br><br>Restore succeeded.<br>% dotnet sln add demo/demo.csproj <br>Project `demo/demo.csproj` added to the solution.<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">建立工程完成。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">下面,增加包<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">mongodb.driver</code>到工程:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">% <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">cd</span> demo<br>% dotnet add package mongodb.driver<br> Determining projects to restore...<br>info : Adding PackageReference <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">for</span> package <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">'mongodb.driver'</span> into project <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">'demo/demo/demo.csproj'</span>.<br>info : Committing restore...<br>info : Writing assets file to disk. Path: demo/demo/obj/project.assets.json<br><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">log</span> : Restored /demo/demo/demo.csproj (<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">in</span> 6.01 sec).<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">项目准备完成。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看一下目录结构:</p>
<pre><code class="bash language-bash hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">% tree .<br>.<br>├── demo<br>│ ├── Program.cs<br>│ ├── demo.csproj<br>│ └── obj<br>│ ├── demo.csproj.nuget.dgspec.json<br>│ ├── demo.csproj.nuget.g.props<br>│ ├── demo.csproj.nuget.g.targets<br>│ ├── project.assets.json<br>│ └── project.nuget.cache<br>└── demo.sln<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">mongodb.driver</code>是MongoDB官方的数据库SDK,从Nuget上安装即可。</em></p>
<h1 id="hdemo" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">三、Demo准备工作</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">创建数据映射的模型类<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel.cs</code>,现在是个空类,后面所有的数据映射相关内容会在这个类进行说明:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">并修改<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Program.cs</code>,准备<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Demo</code>方法,以及连接数据库:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Program</span><br>{</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">const</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> MongoDBConnection = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"mongodb://localhost:27031/admin"</span>;<br><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> IMongoClient _client = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> MongoClient(MongoDBConnection);<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> IMongoDatabase _database = _client.GetDatabase(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Test"</span>);<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> IMongoCollection<CollectionModel> _collection = _database.GetCollection<CollectionModel>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"TestCollection"</span>);<br><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>[] args)</span><br> </span>{<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Demo</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br> Console.ReadKey();<br> }<br><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Demo</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br> </span>{<br> }<br>}<br></code></pre>
<h1 id="h-2" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">四、字段映射</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">从上面的代码中,我们看到,在生成<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Collection</code>对象时,用到了<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">IMongoDatabase _database = _client.GetDatabase(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Test"</span>);<br>IMongoCollection<CollectionModel> _collection = _database.GetCollection<CollectionModel>(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"TestCollection"</span>);<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这两行,其实就完成了一个映射的工作:把<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">MongoDB</code>中,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Test</code>数据库下,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">TestCollection</code>数据集(就是SQL中的数据表),映射到<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>这个数据类中。换句话说,就是用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>这个类,来完成对数据集<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">TestCollection</code>的所有操作。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">保持<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>为空,我们往数据库写入一行数据:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Demo</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> CollectionModel new_item = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> CollectionModel();<br> await _collection.InsertOneAsync(new_item);<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">执行,看一下写入的数据:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"_id"</span> : ObjectId(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"5ef1d8325327fd4340425ac9"</span>)<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">OK,我们已经写进去一条数据了。因为映射类是空的,所以写入的数据,也只有<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>一行内容。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">但是,为什么会有一个<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>呢?</p>
<h2 id="h1id" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">1. ID字段</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">MongoDB数据集中存放的数据,称之为文档(<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Document</code>)。每个文档在存放时,都需要有一个ID,而这个ID的名称,固定叫<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">当我们建立映射时,如果给出<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>字段,则MongoDB会采用这个ID做为这个文档的ID,如果不给出,MongoDB会自动添加一个<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>字段。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">例如:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> ObjectId _id { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">和</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在使用上是完全一样的。唯一的区别是,如果映射类中不写<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>,则MongoDB自动添加<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>时,会用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ObjectId</code>作为这个字段的数据类型。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ObjectId</code>是一个全局唯一的数据。</em></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">当然,MongoDB允许使用其它类型的数据作为ID,例如:<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">string</code>,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">int</code>,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">long</code>,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">GUID</code>等,但这就需要你自己去保证这些数据不超限并且唯一。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">例如,我们可以写成:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">long</span> _id { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们也可以在类中修改<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>名称为别的内容,但需要加一个描述属性<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">BsonId</code>:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> ObjectId topic_id { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这儿特别要注意:<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">BsonId</code>属性会告诉映射,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">topic_id</code>就是这个文档数据的ID。MongoDB在保存时,会将这个<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">topic_id</code>转成<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>保存到数据集中。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><em style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-style: italic">在MongoDB数据集中,ID字段的名称固定叫<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">_id</code>。为了代码的阅读方便,可以在类中改为别的名称,但这不会影响MongoDB中存放的ID名称。</em></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">修改Demo代码:</p>
<pre><code class="C# language-C# hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Demo</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> CollectionModel new_item = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> CollectionModel()<br> {<br> title = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>,<br> content = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span>,<br> };<br> await _collection.InsertOneAsync(new_item);<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">跑一下Demo,看看保存的结果:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"_id"</span> : ObjectId(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"5ef1e1b1bc1e18086afe3183"</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"title"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"content"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span><br>}<br></code></pre>
<h2 id="h2" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">2. 简单字段</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">就是常规的数据字段,直接写就成。</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> ObjectId topic_id { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> favor { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">保存后的数据:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"_id"</span> : ObjectId(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"5ef1e9caa9d16208de2962bb"</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"title"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"content"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"favor"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">100</span>)<br>}<br></code></pre>
<h2 id="h3decimal" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">3. 一个的特殊的类型 - Decimal</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">说Decimal特殊,是因为MongoDB在早期,是不支持Decimal的。直到MongoDB v3.4开始,数据库才正式支持Decimal。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">所以,如果使用的是v3.4以后的版本,可以直接使用,而如果是以前的版本,需要用以下的方式:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> decimal price { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">其实就是把Decimal通过映射,转为Double存储。</p>
<h2 id="h4" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">4. 类字段</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">把类作为一个数据集的一个字段。这是MongoDB作为文档NoSQL数据库的特色。这样可以很方便的把相关的数据组织到一条记录中,方便展示时的查询。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们在项目中添加两个类<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Contact</code>和<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Author</code>:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Contact</span><br>{</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> mobile { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Author</span><br>{</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> name { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> List<Contact> contacts { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">然后,把<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Author</code>加到<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>中:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> ObjectId topic_id { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> favor { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> Author author { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">嗯,开始变得有点复杂了。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">完善Demo代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Demo</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> CollectionModel new_item = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> CollectionModel()<br> {<br> title = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>,<br> content = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span>,<br> favor = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">100</span>,<br> author = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> Author<br> {<br> name = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"WangPlus"</span>,<br> contacts = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> List<Contact>(),<br> }<br> };<br><br> Contact contact_item1 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> Contact()<br> {<br> mobile = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13800000000"</span>,<br> };<br> Contact contact_item2 = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> Contact()<br> {<br> mobile = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13811111111"</span>,<br> };<br> new_item.author.contacts.Add(contact_item1);<br> new_item.author.contacts.Add(contact_item2);<br><br> await _collection.InsertOneAsync(new_item);<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">保存的数据是这样的:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"_id"</span> : ObjectId(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"5ef1e635ce129908a22dfb5e"</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"title"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"content"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"favor"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">100</span>),<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"author"</span> : {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"name"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"WangPlus"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"contacts"</span> : [<br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mobile"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13800000000"</span><br> }, <br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mobile"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13811111111"</span><br> }<br> ]<br> }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这样的数据结构,用着不要太爽!</p>
<h2 id="h5" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">5. 枚举字段</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">枚举字段在使用时,跟类字段相似。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">创建一个枚举<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">TagEnumeration</code>:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">enum</span> TagEnumeration<br>{<br> CSharp = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1</span>,<br> Python = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2</span>,<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">加到<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>中:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> ObjectId topic_id { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> favor { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> Author author { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> TagEnumeration tag { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">修改Demo代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Demo</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> CollectionModel new_item = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> CollectionModel()<br> {<br> title = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>,<br> content = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span>,<br> favor = <span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">100</span>,<br> author = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> Author<br> {<br> name = <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"WangPlus"</span>,<br> contacts = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> List<Contact>(),<br> },<br> tag = TagEnumeration.CSharp,<br> };<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 后边代码略过 */</span><br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">运行后看数据:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"_id"</span> : ObjectId(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"5ef1eb87cbb6b109031fcc31"</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"title"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"content"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"favor"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">100</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"author"</span> : {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"name"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"WangPlus"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"contacts"</span> : [<br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mobile"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13800000000"</span><br> }, <br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mobile"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13811111111"</span><br> }<br> ]<br> }, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"tag"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1</span>)<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在这里,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">tag</code>保存了枚举的值。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们也可以保存枚举的字符串。只要在<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>中,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">tag</code>声明上加个属性:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> ObjectId topic_id { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> favor { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> Author author { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> TagEnumeration tag { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">数据会变成:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"_id"</span> : ObjectId(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"5ef1ec448f1d540919d15904"</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"title"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"content"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"favor"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">100</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"author"</span> : {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"name"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"WangPlus"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"contacts"</span> : [<br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mobile"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13800000000"</span><br> }, <br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mobile"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13811111111"</span><br> }<br> ]<br> }, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"tag"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"CSharp"</span><br>}<br></code></pre>
<h2 id="h6" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">6. 日期字段</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">日期字段会稍微有点坑。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个坑其实并不源于MongoDB,而是源于C#的<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DateTime</code>类。我们知道,时间根据时区不同,时间也不同。而<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DateTime</code>并不准确描述时区的时间。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我们先在<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>中增加一个时间字段:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">CollectionModel</span><br>{</span><br> <br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> ObjectId topic_id { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> content { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> favor { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> Author author { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> TagEnumeration tag { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> DateTime post_time { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">修改Demo:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">private</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Demo</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span><br></span>{<br> CollectionModel new_item = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> CollectionModel()<br> {<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 前边代码略过 */</span><br> post_time = DateTime.Now, <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 2020-06-23T20:12:40.463+0000 */</span><br> };<br> <span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(128, 128, 128, 1); word-wrap: inherit !important; word-break: inherit !important">/* 后边代码略过 */</span><br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">运行看数据:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"_id"</span> : ObjectId(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"5ef1f1b9a75023095e995d9f"</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"title"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"content"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"Demo content"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"favor"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">100</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"author"</span> : {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"name"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"WangPlus"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"contacts"</span> : [<br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mobile"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13800000000"</span><br> }, <br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mobile"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"13811111111"</span><br> }<br> ]<br> }, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"tag"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"CSharp"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"post_time"</span> : ISODate(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"2020-06-23T12:12:40.463+0000"</span>)<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">对比代码时间和数据时间,会发现这两个时间差了8小时 - 正好的中国的时区时间。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">MongoDB规定,在数据集中存储时间时,只会保存UTC时间。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">如果只是保存(像上边这样),或者查询时使用时间作为条件(例如查询<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">post_time < DateTime.Now</code>的数据)时,是可以使用的,不会出现问题。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">但是,如果是查询结果中有时间字段,那这个字段,会被<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DateTime</code>默认设置为<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DateTimeKind.Unspecified</code>类型。而这个类型,是无时区信息的,输出显示时,会造成混乱。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">为了避免这种情况,在进行时间字段的映射时,需要加上属性:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> DateTime post_time { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这样做,会强制<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DateTime</code>类型的字段为<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DateTimeKind.Local</code>类型。这时候,从显示到使用就正确了。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">但是,别高兴的太早,这儿还有一个但是。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个但是是这样的:数据集中存放的是UTC时间,跟我们正常的时间有8小时时差,如果我们需要按日统计,比方每天的销售额/点击量,怎么搞?上面的方式,解决不了。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">当然,基于MongoDB自由的字段处理,可以把需要统计的字段,按年月日时分秒拆开存放,像下面这样的:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Post_Time</span><br>{</span><br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> year { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> month { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> day { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> hour { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> minute { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> second { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">能解决,但是Low哭了有没有?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">下面,终极方案来了。它就是:改写MongoDB中对于<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DateTime</code>字段的序列化类。当当当~~~</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">先创建一个类<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">MyDateTimeSerializer</code>:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">class</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">MyDateTimeSerializer</span> :</span> DateTimeSerializer<br>{<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> override DateTime <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Deserialize</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(BsonDeserializationContext context, BsonDeserializationArgs args)</span><br> </span>{<br> var obj = base.Deserialize(context, args);<br> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">return</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(obj.Ticks, DateTimeKind.Unspecified);<br> }<br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> override <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">void</span> <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Serialize</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(BsonSerializationContext context, BsonSerializationArgs args, DateTime value)</span><br> </span>{<br> var utcValue = <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> DateTime(value.Ticks, DateTimeKind.Utc);<br> base.Serialize(context, args, utcValue);<br> }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">代码简单,一看就懂。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><strong style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0; font-weight: bold">注意,使用这个方法,上边那个对于时间加的属性<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)"></code>一定不要添加,要不然就等着哭吧:P</strong></p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">创建完了,怎么用?</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">如果你只想对某个特定映射的特定字段使用,比方只对<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">CollectionModel</code>的<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">post_time</code>字段来使用,可以这么写:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> DateTime post_time { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">或者全局使用:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">BsonSerializer.RegisterSerializer(typeof(DateTime), <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> MongoDBDateTimeSerializer());<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">BsonSerializer</code>是MongoDB.Driver的全局对象。所以这个代码,可以放到使用数据库前的任何地方。例如在Demo中,我放在<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Main</code>里了:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">static</span> async Task <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Main</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">(<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>[] args)</span><br></span>{<br> BsonSerializer.RegisterSerializer(typeof(DateTime), <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">new</span> MyDateTimeSerializer());<br><br> <span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">await <span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">Demo</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(255, 152, 35, 1); word-wrap: inherit !important; word-break: inherit !important">()</span></span>;<br> Console.ReadKey();<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这回看数据,数据集中的<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">post_time</code>跟当前时间显示完全一样了,你统计,你分组,可以随便霍霍了。</p>
<h2 id="h7dictionary" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">7. Dictionary字段</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个需求很奇怪。我们希望在一个Key-Value的文档中,保存一个Key-Value的数据。但这个需求又是真实存在的,比方保存一个用户的标签和标签对应的命中次数。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">数据声明很简单:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> Dictionary<<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>, <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span>> extra_info { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">MongoDB定义了三种保存属性:<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Document</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ArrayOfDocuments</code>、<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ArrayOfArrays</code>,默认是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Document</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">属性写法是这样的:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> Dictionary<<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span>, <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span>> extra_info { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这三种属性下,保存在数据集中的数据结构有区别。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DictionaryRepresentation.Document</code>:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"extra_info"</span> : {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"type"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1</span>), <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"mode"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2</span>)<br> }<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DictionaryRepresentation.ArrayOfDocuments</code>:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"extra_info"</span> : [<br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"k"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"type"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"v"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1</span>)<br> }, <br> {<br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"k"</span> : <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"mode"</span>, <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"v"</span> : NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2</span>)<br> }<br> ]<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DictionaryRepresentation.ArrayOfArrays</code>:</p>
<pre><code class="json language-json hljs" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important">{ <br> <span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(165, 218, 45, 1); word-wrap: inherit !important; word-break: inherit !important">"extra_info"</span> : [<br> [<br> <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"type"</span>, <br> NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">1</span>)<br> ], <br> [<br> <span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(238, 220, 112, 1); word-wrap: inherit !important; word-break: inherit !important">"mode"</span>, <br> NumberInt(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(174, 135, 250, 1); word-wrap: inherit !important; word-break: inherit !important">2</span>)<br> ]<br> ]<br>}<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这三种方式,从数据保存上并没有什么区别,但从查询来讲,如果这个字段需要进行查询,那三种方式区别很大。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">如果采用BsonDocument方式查询,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DictionaryRepresentation.Document</code>无疑是写着最方便的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">如果用Builder方式查询,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DictionaryRepresentation.ArrayOfDocuments</code>是最容易写的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"><code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DictionaryRepresentation.ArrayOfArrays</code>就算了。数组套数组,查询条件写死人。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">我自己在使用时,多数情况用<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">DictionaryRepresentation.ArrayOfDocuments</code>。</p>
<h1 id="h-3" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">五、其它映射属性</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">上一章介绍了数据映射的完整内容。除了这些内容,MongoDB还给出了一些映射属性,供大家看心情使用。</p>
<h2 id="h1bsonelement" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">1. BsonElement属性</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个属性是用来改数据集中的字段名称用的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> DateTime post_time { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">在不加<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">BsonElement</code>的情况下,通过数据映射写到数据集中的文档,字段名就是变量名,上面这个例子,字段名就是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">post_time</code>。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">加上<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">BsonElement</code>后,数据集中的字段名会变为<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">pt</code>。</p>
<h2 id="h2bsondefaultvalue" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">2. BsonDefaultValue属性</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看名称就知道,这是用来设置字段的默认值的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> title { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">当写入的时候,如果映射中不传入值,则数据库会把这个默认值存到数据集中。</p>
<h2 id="h3bsonrepresentation" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">3. BsonRepresentation属性</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个属性是用来在映射类中的数据类型和数据集中的数据类型做转换的。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">int</span> favor { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这段代表表示,在映射类中,<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">favor</code>字段是<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">int</code>类型的,而存到数据集中,会保存为<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">string</code>类型。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">前边<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">Decimal</code>转换和枚举转换,就是用的这个属性。</p>
<h2 id="h4bsonignore" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.4em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">4. BsonIgnore属性</span></h2>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这个属性用来忽略某些字段。忽略的意思是:映射类中某些字段,不希望被保存到数据集中。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">看代码:</p>
<pre><code class="c# language-c# hljs cpp" style="margin: 0 2px; line-height: 18px; font-size: 14px; font-weight: normal; word-spacing: 0; letter-spacing: 0; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0; color: rgba(169, 183, 198, 1); background-color: rgba(40, 43, 46, 1); overflow-x: auto; padding: 0.5em; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow: auto !important"><br><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">public</span> <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">string</span> ignore_string { get; <span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0; padding: 0; color: rgba(248, 35, 117, 1); word-wrap: inherit !important; word-break: inherit !important">set</span>; }<br></code></pre>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">这样,在保存数据时,字段<code style="font-size: inherit; line-height: inherit; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; color: rgba(233, 105, 0, 1); background-color: rgba(248, 248, 248, 1)">ignore_string</code>就不会被保存到数据集中。</p>
<h1 id="h-4" style="color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0; font-weight: bold; font-size: 1.6em"><span style="font-size: inherit; color: inherit; line-height: inherit; margin: 0; padding: 0">六、总结</span></h1>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">数据映射本身没什么新鲜的内容,但在MongoDB中,如果用好了映射,开发过程从效率到爽的程度,都不是SQL可以相比的。正所谓:</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">一入Mongo深似海,从此SQL是路人。</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">谢谢大家!</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">(全文完)</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0">本文的配套代码在https://github.com/humornif/Demo-Code/tree/master/0015/demo</p>
<p style="font-size: inherit; color: inherit; line-height: inherit; padding: 0; margin: 1.5em 0"> </p>
<hr>
<p> </p>
<table border="0">
<tbody>
<tr>
<td><img src="https://img2020.cnblogs.com/blog/907112/202005/907112-20200527230728396-985375280.jpg"></td>
<td>
<p>微信公众号:老王Plus</p>
<p>扫描二维码,关注个人公众号,可以第一时间得到最新的个人文章和内容推送</p>
<p>本文版权归作者所有,转载请保留此声明和原文链接</p>
</td>
</tr>
</tbody>
</table>
</div><br><br>
来源:https://www.cnblogs.com/tiger-wang/p/13185605.html
頁:
[1]