花无心 發表於 2022-6-10 08:19:00

MongoDB +MongoDB在.net Core中的应用+封装MongoDB上下文类

<h1>MongoDB +MongoDB在.net Core中的应用</h1>
<h2>一、什么是MongoDB</h2>
<ul>
<li>MongoDB是一个基于<code>分布式文件存储</code>的数据库</li>
<li>MongoDB是一个介于<code>关系数据库</code>和<code>非关系数据库</code>之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。</li>
</ul>
<p>&nbsp;  MonfoDB创建新连接参考:</p>
<p><span style="background-color: rgba(255, 255, 0, 1)">https://blog.csdn.net/lavendersue/article/details/104924258?spm=1001.2101.3001.6650.1&amp;utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-104924258-blog-100642715.pc_relevant_antiscanv4&amp;depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-104924258-blog-100642715.pc_relevant_antiscanv4&amp;utm_relevant_index=1</span></p>
<p>   <strong><span style="font-size: 16px">创建一个新的连接</span></strong></p>
<p><strong><span style="font-size: 16px">  1、</span></strong><span style="font-size: 16px">任意打开一个调出可以编写代码的地方</span></p>
<p><span style="font-size: 16px">  <img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220609213227742-1456780935.png"></span></p>
<p>&nbsp;</p>
<p>&nbsp;创建一个用户:</p>
<p> <img src="https://img2022.cnblogs.com/blog/2803250/202207/2803250-20220704171231447-484109522.png"></p>
<p>--没有会引起错误:&nbsp;Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.</p>
<p><img src="https://img2022.cnblogs.com/blog/2803250/202207/2803250-20220704171259289-1144480366.png"></p>
<p>&nbsp;</p>
<p> </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>二、MongoDB在.net Core 6.0中的应用</h2>
<p>  .net core操作mongoDB</p>
<p>  <span style="background-color: rgba(255, 255, 0, 1)">https://blog.csdn.net/only_yu_yy/article/details/78882446</span></p>
<p><em><span style="font-size: 18pt">1、</span></em><span style="font-size: 18pt">编写基层类,用于生成对应编辑库的MongoDB上下文类使用(不用ef那种dbset&lt;&gt;了)--并安装所需包</span></p>
<p><span style="font-size: 18pt">  <span style="font-size: 18px; color: rgba(0, 128, 128, 1)">//要这些基础的设施是因为ef他原本就封好了,但是MongoDB没有所以需要我们自己写下</span></span></p>
<p><span style="color: rgba(255, 0, 0, 1)">  MongoDBContextOptions</span></p>
<p>&nbsp;</p>
<p>  <span style="font-size: 14px">生成一个用与存放字符串信息与所要连接的MongoDB数据库名称信息的类---对应这里的信息数据是通过后面和ef上下文/dapper上下文在一起的那个类通过构造传来的</span></p>
<p><span style="font-size: 14px"><img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220609193920864-640397795.png"></span></p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoDBContextOptions
    {
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> Configuration { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span>; }<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">连接MongoDB字符串</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> DatabaseName { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span>; } <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">连接对应的数据库名称--MongoDB需要指定连接的数据库</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> MongoDBContextOptions Value { <span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">; } }
    }</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="color: rgba(255, 0, 0, 1)">  MongoDBContext</span></p>
<p>  根据MongoDBContextOptions类中的信息,配置上下文,起连接,取连接的数据库--这样目的就是实现我们为什么在ef中直接用上下文.表.add就可以对对应数据库,对应表实现添加</p>
<p><img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220609202915303-450152447.png"></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoDBContext
    {
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">此构造方法主要目的是接连接字符串的配置和要操作哪个数据库
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">安装包MongoDB.Driver</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span> MongoClient _mongoClent;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mongodb的连接</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">readonly</span> MongoDBContextOptions _options;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使用强类型来接受配置信息</span>
      <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> IMongoDatabase _database;

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 构造函数中接收传过来的配置信息(连接字符串+操作的对应数据库)options
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="optionsAccessor"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">protected</span> MongoDBContext(IOptions&lt;MongoDBContextOptions&gt;<span style="color: rgba(0, 0, 0, 1)"> optionsAccessor)
      {
            _options </span>=<span style="color: rgba(0, 0, 0, 1)"> optionsAccessor.Value;
            _mongoClent </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> MongoClient(_options.Configuration);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过options中存放的连接字符串, 起连接</span>
            _database = _mongoClent.GetDatabase(_options.DatabaseName); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过options中存放的数据库名称,获取数据库</span>
<span style="color: rgba(0, 0, 0, 1)">

      }
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 获取mongodb 的连接
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> MongoClient MongoClent { <span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> _mongoClent; } }

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 获取连接的数据库
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> IMongoDatabase Database { <span style="color: rgba(0, 0, 255, 1)">get</span> { <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> _database; } }
    }</span></pre>
</div>
<p>  </p>
<p>  </p>
<p><span style="color: rgba(255, 0, 0, 1)">  MongoDBContextServiceCollectionExtensions</span></p>
<p>  一个扩展,用于把MongoDB注册到容器中&nbsp;</p>
<p>  --因为在Programe需要注册,并且要把连接字符串给带过来,所以写个扩展方法用于注册(<span style="color: rgba(128, 128, 0, 1)">与ef注册时用的builder.ServicesAddDbContext()一个东西,只不过人家封装好了的</span>)<img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220609205126679-800689441.png"></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> MongoDBContextServiceCollectionExtensions
    {
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 是一个扩展,用于把MongoDB注册到容器中
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;typeparam name="T"&gt;&lt;/typeparam&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="services"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="setupAction"&gt;&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;exception cref="ArgumentNullException"&gt;&lt;/exception&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> IServiceCollection AddMongoDBContext&lt;T&gt;(<span style="color: rgba(0, 0, 255, 1)">this</span> IServiceCollection services, Action&lt;MongoDBContextOptions&gt; setupAction) <span style="color: rgba(0, 0, 255, 1)">where</span><span style="color: rgba(0, 0, 0, 1)"> T : MongoDBContext
      {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (services == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
            { </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArgumentNullException(nameof(services)); }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (setupAction == <span style="color: rgba(0, 0, 255, 1)">null</span>) { <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArgumentNullException(nameof(setupAction)); }
            services.Configure(setupAction);
            services.AddScoped</span>&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)">();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> services;
      }
    }</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">2、根据当前的需求生成一个用于对跟踪信息中所需的经、纬度、合同等信息编辑的MongoDB上下文</span></p>
<p><span style="font-size: 18pt"> &nbsp; --<span style="font-size: 18px">这一步操作就相当于ef中在一个dbcontext中很多个dbset&lt;&gt;,然后用的时候调用对应的映射--实现对对应表操作</span><br></span></p>
<p><span style="font-size: 18pt">&nbsp; &nbsp; <span style="font-size: 14px">&nbsp;--而此时在MongoDB这里就是跟据前面配置的连接字符串/与库,传给上面的MongoDBContextOptions类,再由其一层一层传封装好MongoDB上下文就可以直接对对应库表编辑了</span></span><img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220609210704487-189088896.png"></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
    <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> mongoDb上下文   合同库的实现
    </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ContractDbMongoDBContext : MongoDBContext
    {
      </span><span style="color: rgba(0, 0, 255, 1)">public</span> ContractDbMongoDBContext(IOptions&lt;MongoDBContextOptions&gt; optionsAccessor) : <span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)">(optionsAccessor)
      {
      }
    }</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">3、在Progame中注册下合同MongoDB上下文</span></p>
<p><span style="font-size: 18pt">  <img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220609212054456-1261284739.png"></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注册mongodb上下文</span>
    builder.Services.AddMongoDBContext&lt;ContractDbMongoDBContext&gt;(options =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      options.Configuration </span>= builder.Configuration.GetSection(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MyConfig</span><span style="color: rgba(128, 0, 0, 1)">"</span>)[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MongoDBConnection</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];
      options.DatabaseName </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">contract_db</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
    });</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">4、appsettings中编写MongoDB连接字符串</span><span style="font-size: 18pt"> <img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220609212438133-522611331.png"></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">MongoDBConnection</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">mongodb://admin:123456@127.0.0.1:27017</span><span style="color: rgba(128, 0, 0, 1)">"</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">5、注入MongoDB上下文</span></p>
<p><span style="font-size: 18pt">  <span style="font-size: 15px">1、注入上下文类</span></span></p>
<p><span style="font-size: 18pt"><img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220609222520331-1763533434.png"></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">    private</span><span style="color: rgba(0, 0, 0, 1)"> ContractDbMongoDBContext _ContractDbMongoDBContext;

      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 注入上下文类
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="db"&gt;</span><span style="color: rgba(0, 128, 0, 1)">ef类上下文类</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="ContractDbMongoDBContexts"&gt;</span><span style="color: rgba(0, 128, 0, 1)">MongoDb上下文类</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> ShipperContractRepository(ProjectDbContext db, ContractDbMongoDBContext ContractDbMongoDBContexts) : <span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)">(db)
      {
            _ContractDbMongoDBContext </span>=<span style="color: rgba(0, 0, 0, 1)"> ContractDbMongoDBContexts;
      }</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">6、使用MongoDB</span><span style="font-size: 18pt">  </span></p>
<p>  .net core操作mongoDB<br>  <span style="background-color: rgba(255, 255, 0, 1)">https://blog.csdn.net/only_yu_yy/article/details/78882446</span></p>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">  <span style="font-size: 15px">1、建立对应所需model</span></span></p>
<p><span style="font-size: 18pt"><span style="font-size: 15px">    <img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220610075610139-127681625.png"></span></span></p>
<p>&nbsp;</p>
<p>  2.、对应仓储层实现对MongoDB的操作--插入数据</p>
<p>  <img src="https://img2022.cnblogs.com/blog/2803250/202206/2803250-20220610081147849-493902705.png"></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)">//合同跟踪给Mongodb添加数据添加运输合同车辆坐标
      </span><span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="cId"&gt;</span><span style="color: rgba(0, 128, 0, 1)">合同id</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="lng"&gt;</span><span style="color: rgba(0, 128, 0, 1)">经度</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="lat"&gt;</span><span style="color: rgba(0, 128, 0, 1)">维度</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
      <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> ContractFreightLineLngAndLatAdd(<span style="color: rgba(0, 0, 255, 1)">int</span> cId, <span style="color: rgba(0, 0, 255, 1)">double</span> lng, <span style="color: rgba(0, 0, 255, 1)">double</span><span style="color: rgba(0, 0, 0, 1)"> lat)
      {

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1、从MongoDB上下文获取或者创建放置对应信息的库(sql中的库)
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1-1数据库不存在,也没有关系,它会在首次使用数据库的时候进行自动创建。</span>
            <span style="color: rgba(0, 0, 255, 1)">var</span> db =<span style="color: rgba(0, 0, 0, 1)"> _ContractDbMongoDBContext.Database;

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取或者创建线路跟踪集合
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2、我们可以调用database的GetCollection&lt;TDocument&gt; 方法来获取数据集,(sql中的表)
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2-1其中如果数据是预先定义好的可以在&lt;输入数据的类型&gt;,如果是没有定义好的,
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">可以使用BsonDocument类型,BsonDocument表示没有预定于的模式。
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2-2我们将获取到上面“db”所对应的数据库中的“freight_line”集合,
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">即使“freight_line”集合不存在也没有关系,
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">同数据库一样,若数据集不存在,会自动创建该数据集。</span>
            <span style="color: rgba(0, 0, 255, 1)">var</span> collection = db.GetCollection&lt;ContractFreightLine&gt;(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">freight_line</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">实例一个集合用于添加</span>
            ContractFreightLine ContractFreightLineInfo = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ContractFreightLine()
            {
                id </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ObjectId(),
                cid </span>=<span style="color: rgba(0, 0, 0, 1)"> cId,
                lng </span>=<span style="color: rgba(0, 0, 0, 1)"> lng,
                lat </span>=<span style="color: rgba(0, 0, 0, 1)"> lat,
                created_date </span>=<span style="color: rgba(0, 0, 0, 1)"> DateTime.Now
            };
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据MongoDB上下文插入数据--向集合中插入数据
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">InsertOne(同步插入)或InsertOneAsync(异步插入)方法。</span>
<span style="color: rgba(0, 0, 0, 1)">            collection.InsertOne(ContractFreightLineInfo);

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回主键ID</span>
            <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> ContractFreightLineInfo.id.ToString();
      }</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">&nbsp;</span></p>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:じ逐梦,转载请注明原文链接:https://www.cnblogs.com/ZhuMeng-Chao/p/16359922.html</p><br><br>
来源:https://www.cnblogs.com/ZhuMeng-Chao/p/16359922.html
頁: [1]
查看完整版本: MongoDB +MongoDB在.net Core中的应用+封装MongoDB上下文类