通C#访问MongoDB数据

webphp 13年前
     <div id="article_content" class="article_content">     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">开始:</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">先下载个C#的驱动。MongoDB提供各种主流与非主流预言的开发驱动。</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">C# Driver 下载地址:<a style="color:#6fbc4c;" title="猛击这里" href="/misc/goto?guid=4959500788589786922">猛击这里</a></p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">CSharp Driver Tutorial:<a style="color:#6fbc4c;" href="/misc/goto?guid=4959500788677835777">猛击这里</a></p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">下载文件安装或者解压缩包</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">如果您是安装,请到安装位置寻找,如果是ZIP压缩包,解压缩包得到如下两个文件:</p>     <ol style="line-height:19px;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;margin-left:2em;">      <li style="list-style-type:decimal;">MongoDB.Bson.dll              :序列化、Json相关</li>      <li style="list-style-type:decimal;">MongoDB.Driver.dll             :我们的驱动</li>     </ol>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">没了,只有这两个文件是我们的最爱。</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;"> </p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">继续:</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">新建一个C#的项目,不管你是vs2008,还是vs2010,也许您手中有vs2012?也发给我一份吧。感谢共享:)</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">添加引用,将上面两个DLL引入到项目里面</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;"><img style="border-right-width:0px;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" border="0" alt="通C#访问MongoDB数据" src="https://simg.open-open.com/show/d6511222ac7a1b6546bea29a2e00030d.jpg" width="363" height="396" /></p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;"> </p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">您启动Mongod.exe了吗?启动服务。</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;"> </p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">代码里面添加命名空间:</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">using MongoDB.Bson;<br /> using MongoDB.Driver;</p>     <p style="line-height:19px;margin:5px auto;font-family:verdana,Arial,helvetica,sans-seriff;color:#4b4b4b;">跟着[CSharp Driver Tutorial:<a style="color:#6fbc4c;" href="/misc/goto?guid=4959500788677835777">猛击这里</a>]继续做。如果您没有打开[CSharp Driver Tutorial]就不要开了,看完全文再看,以免分散精力。</p>     <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code">      <div>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> MongoDB连接串,以[mongodb:</span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;">]开头。这里,我们连接的是本机的服务</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;color:#0000ff;">string</span>       <span style="line-height:1.5;"> connectionString </span>       <span style="line-height:1.5;">=</span>       <span style="line-height:1.5;"> </span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">mongodb://localhost</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">;<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 连接到一个MongoServer上</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;">MongoServer server </span>       <span style="line-height:1.5;">=</span>       <span style="line-height:1.5;"> MongoServer.Create(connectionString);</span>      </div>     </div>     <ol style="margin-left:2em;">      <li style="list-style-type:decimal;">MongoDb的连接串<br />       <ul style="list-style-type:disc;margin:0px 0px 10px 45px;padding-left:5px;font-size:11px;">        <li style="list-style-type:decimal;">在连接串中,我们可以指定其他机器上的服务和连接端口格式如下:</li>        <li style="list-style-type:decimal;">mongodb://[username:password@]hostname[:port][/[database][?options]]</li>        <li style="list-style-type:decimal;">简单示例:mongodb://server1,server2:27017,server2:27018</li>        <li style="list-style-type:decimal;">更进一步详细的信息请查看[CSharp Driver Tutorial:<a style="color:#6fbc4c;" href="/misc/goto?guid=4959500788677835777">猛击这里</a>]</li>       </ul> </li>      <li style="list-style-type:decimal;">MongoServer<br />       <ul style="list-style-type:disc;margin:0px 0px 10px 45px;padding-left:5px;font-size:11px;">        <li style="list-style-type:decimal;">有几种不同的重载了的创建方式:</li>        <li style="list-style-type:decimal;">MongoServer Create()   如果只是连接本机,并且本机只启动了一个服务,直接调用这个方法,完全不需要连接串</li>        <li style="list-style-type:decimal;">MongoServer Create(MongoConnectionStringBuilder builder)</li>        <li style="list-style-type:decimal;">MongoServer Create(MongoUrl url)</li>        <li style="list-style-type:decimal;">MongoServer Create(string connectionString)</li>        <li style="list-style-type:decimal;">MongoServer Create(Uri uri)</li>        <li style="list-style-type:decimal;">更进一步详细的信息请查看[CSharp Driver Tutorial:<a style="color:#6fbc4c;" href="/misc/goto?guid=4959500788677835777">猛击这里</a>]</li>       </ul> </li>     </ol>     <p style="margin:5px auto;">继续:</p>     <p style="margin:5px auto;">再增加几行代码: </p>     <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code">      <div id="cnblogs_code_open_b70a4a0d-d5a3-442c-aaa2-de34c6f4e08b">       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> MongoDB连接串,以[mongodb:</span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;">]开头。这里,我们连接的是本机的服务</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;color:silver;">string</span>       <span style="line-height:1.5;color:silver;"> connectionString </span>       <span style="line-height:1.5;color:silver;">=</span>       <span style="line-height:1.5;color:silver;"> </span>       <span style="line-height:1.5;color:silver;">"</span>       <span style="line-height:1.5;color:silver;">mongodb://localhost</span>       <span style="line-height:1.5;color:silver;">"</span>       <span style="line-height:1.5;">;<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 连接到一个MongoServer上</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;color:silver;">MongoServer server </span>       <span style="line-height:1.5;color:silver;">=</span>       <span style="line-height:1.5;color:silver;"> MongoServer.Create(connectionString);            <br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> -------------------------------------------------------------------------<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 打开数据库testdb</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;">MongoDatabase db </span>       <span style="line-height:1.5;">=</span>       <span style="line-height:1.5;"> server.GetDatabase(</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">testdb</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">);<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 获取集合employees</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;">MongoCollection collection </span>       <span style="line-height:1.5;">=</span>       <span style="line-height:1.5;"> db.GetCollection(</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">employees</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">);</span>      </div>     </div>     <ol style="margin-left:2em;">      <li style="list-style-type:decimal;">server.GetDatabase(<span style="color:#800000;">"</span><span style="color:#800000;">testdb</span><span style="color:#800000;">"</span>)       <ul style="list-style-type:disc;margin:0px 0px 10px 45px;padding-left:5px;font-size:11px;">        <li style="list-style-type:decimal;">打开数据库:testdb</li>        <li style="list-style-type:decimal;">我没有testdb库啊??不要担心,不要疑惑,不要在这个问题上浪费时间,如果没有这个库存在,MongoDB会自动替你创建它的</li>       </ul> </li>      <li style="list-style-type:decimal;">db.GetCollection(<span style="color:#800000;">"</span><span style="color:#800000;">employees</span><span style="color:#800000;">"</span>)       <ul style="list-style-type:disc;margin:0px 0px 10px 45px;padding-left:5px;font-size:11px;">        <li style="list-style-type:decimal;">获取集合:employees</li>        <li style="list-style-type:decimal;">好吧有了前一个教训,管他呢,爱存在不存在,MongoDB的开发人员都会替我们创建吧?</li>       </ul> </li>     </ol>     <p style="margin:5px auto;">继续: </p>     <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code">      <div>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> MongoDB连接串,以[mongodb:</span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;">]开头。这里,我们连接的是本机的服务</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;color:silver;">string</span>       <span style="line-height:1.5;color:silver;"> connectionString </span>       <span style="line-height:1.5;color:silver;">=</span>       <span style="line-height:1.5;color:silver;"> </span>       <span style="line-height:1.5;color:silver;">"</span>       <span style="line-height:1.5;color:silver;">mongodb://localhost</span>       <span style="line-height:1.5;color:silver;">"</span>       <span style="line-height:1.5;">;<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 连接到一个MongoServer上</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;color:silver;">MongoServer server </span>       <span style="line-height:1.5;color:silver;">=</span>       <span style="line-height:1.5;color:silver;"> MongoServer.Create(connectionString);            <br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> -------------------------------------------------------------------------<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 打开数据库testdb</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;color:silver;">MongoDatabase db </span>       <span style="line-height:1.5;color:silver;">=</span>       <span style="line-height:1.5;color:silver;"> server.GetDatabase(</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">testdb</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:silver;">);<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 获取集合employees</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;color:silver;">MongoCollection collection </span>       <span style="line-height:1.5;color:silver;">=</span>       <span style="line-height:1.5;color:silver;"> db.GetCollection(</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">employees</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:silver;">);</span>      </div>      <div>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> -------------------------------------------------------------------------            <br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 创建一个employee</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;">BsonDocument employee </span>       <span style="line-height:1.5;">=</span>       <span style="line-height:1.5;"> </span>       <span style="line-height:1.5;color:#0000ff;">new</span>       <span style="line-height:1.5;"> BsonDocument <br /> {<br />   { </span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">name</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">, </span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">Ernest Hemingway</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;"> },<br />   { </span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">title</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">, </span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">For Whom the Bell Tolls</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;"> }<br /> };<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 把它写到上面那个集合里面去</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;">collection.Insert(employee);</span>      </div>     </div>     <p style="margin:5px auto;"> 如果您的程序没有抛出任何异常,那么数据已经进去了。不知到BsonDocument是个啥东西?</p>     <p style="margin:5px auto;">看个简单的例子吧:</p>     <p style="margin:5px auto;">BsonDocument document = <span class="code-keyword">new</span> BsonDocument {<br />         { <span class="code-quote">"name"</span>, name },<br />         { <span class="code-quote">"city"</span>, city }, <span class="code-comment">// not added <span class="code-keyword">if</span> city is <span class="code-keyword">null</span><br /> </span>        { <span class="code-quote">"dob"</span>, dob, dobAvailable } <span class="code-comment">// not added <span class="code-keyword">if</span> dobAvailable is <span class="code-keyword">false</span><br /> </span>    };<br /> 它以键值对的方式、JSON的格式,保存数据。MongoDB通过BsonDocument套BsonDocument的方式,使您可以存储复杂格式的数据。</p>     <ul style="margin:0px 0px 10px 45px;padding-left:5px;font-size:11px;">      <li>有一些重要的概念,看完文章以后,你一定要去过一遍:BsonType、BsonValue、BsonElement、BsonDocument、MongoServer、MongoDatabase、MongoCollection</li>      <li>在这里: [CSharp Driver Tutorial:<a style="color:#6fbc4c;" href="/misc/goto?guid=4959500788677835777">猛击这里</a>]</li>     </ul>     <p style="margin:5px auto;"> 继续:</p>     <p style="margin:5px auto;">最后几行小代码:</p>     <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:5px;overflow-x:auto;overflow-y:auto;background-color:#f5f5f5;padding-left:5px;padding-right:5px;font-family:'Courier New';word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:5px;" class="cnblogs_code">      <div>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> -------------------------------------------------------------------------<br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 查询上面那个刚刚插进去的数据,就这格式了,看看QueryDocument的文档吧</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;">var query </span>       <span style="line-height:1.5;">=</span>       <span style="line-height:1.5;"> </span>       <span style="line-height:1.5;color:#0000ff;">new</span>       <span style="line-height:1.5;"> QueryDocument(</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">name</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">, </span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">Ernest Hemingway</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">);            <br /> </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> 遍历结果</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;color:#0000ff;">foreach</span>       <span style="line-height:1.5;"> (BsonDocument emp </span>       <span style="line-height:1.5;color:#0000ff;">in</span>       <span style="line-height:1.5;"> collection.Find(query))<br /> {<br />     </span>       <span style="line-height:1.5;color:#008000;">//</span>       <span style="line-height:1.5;color:#008000;"> BsonValue有两种取值方式,下面两个都用了一个是AsXXX,一个是ToXXX</span>       <span style="line-height:1.5;color:#008000;"><br /> </span>       <span style="line-height:1.5;">    Console.WriteLine(</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">name:{0}\ttitle:{1}</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">, emp[</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">name</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">].AsString, emp[</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;color:#800000;">title</span>       <span style="line-height:1.5;color:#800000;">"</span>       <span style="line-height:1.5;">].ToString());<br /> }</span>      </div>     </div>     <ol style="margin-left:2em;">      <li style="list-style-type:decimal;">QueryDocument       <ul style="list-style-type:disc;margin:0px 0px 10px 45px;padding-left:5px;font-size:11px;">        <li style="list-style-type:decimal;">MongoCollection         <bsondocument>          books;          <br />               <span class="code-keyword">var</span> query = Query.And(          <br />         Query.EQ(          <span class="code-quote">"author"</span>,           <span class="code-quote">"Kurt Vonnegut"</span>),          <br />         Query.EQ(          <span class="code-quote">"title"</span>,           <span class="code-quote">"Cats Craddle"</span>)          <br />     );          <br />         </bsondocument></li>        <li style="list-style-type:decimal;">不解释了,查询的各种细节用法看各种文档吧(因为我还没看呢,解释不了,呵呵)。作为第一步,这里知道有这个东西,量和细节的积累在不断的实践中获得。[CSharp Driver Tutorial:<a style="color:#6fbc4c;" href="/misc/goto?guid=4959500788677835777">猛击这里</a>]</li>       </ul> </li>      <li style="list-style-type:decimal;">BsonValue 的取值       <ul style="list-style-type:disc;margin:0px 0px 10px 45px;padding-left:5px;font-size:11px;">        <li style="list-style-type:decimal;">emp[<span style="color:#800000;">"</span><span style="color:#800000;">name</span><span style="color:#800000;">"</span>].AsString 这是第一种取值方式。相关的有AsInt32,AsBoolean等等</li>        <li style="list-style-type:decimal;">emp[<span style="color:#800000;">"</span><span style="color:#800000;">title</span><span style="color:#800000;">"</span>].ToString() 这是第二种取值方式。相关的有ToInt32,ToBoolean等等</li>        <li style="list-style-type:decimal;">请注意细节:</li>        <li style="list-style-type:decimal;">AsXXX取值方式,如果类型不一致,可以抛出异常InvalidCastException</li>        <li style="list-style-type:decimal;">ToXXX取值方式,不会抛出异常,会返回默认值。[CSharp Driver Tutorial:<a style="color:#6fbc4c;" href="/misc/goto?guid=4959500788677835777">猛击这里</a>]</li>       </ul> </li>     </ol>     <p style="margin:5px auto;">至此,已经完成第一次C#程序访问MongoDB的全过程。</p>     <p style="margin:5px auto;">PS:重要概念:</p>     <p style="margin:5px auto;">BsonType、BsonValue、BsonElement、BsonDocument、MongoServer、MongoDatabase、MongoCollection</p>     <p style="margin:5px auto;">一定要看。[CSharp Driver Tutorial:<a style="color:#6fbc4c;" href="/misc/goto?guid=4959500788677835777">猛击这里</a>]</p>    </div>