时间:2021-07-01 10:21:17 帮助过:19人阅读
安装nuget包(或者执行命令 Install-Package MongoDB.Driver)
然后就可以使用C#操作MongoDB数据库了,看一个添加的demo:
- <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
- {
- </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
- {
- </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">待添加的document</span>
- <span style="color: #0000ff;">var</span> doc = <span style="color: #0000ff;">new</span><span style="color: #000000;"> BsonDocument{
- { </span><span style="color: #800000;">"</span><span style="color: #800000;">_id</span><span style="color: #800000;">"</span>,<span style="color: #800080;">7</span><span style="color: #000000;"> },
- { </span><span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">吴九</span><span style="color: #800000;">"</span><span style="color: #000000;"> },
- { </span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">29</span><span style="color: #000000;"> },
- { </span><span style="color: #800000;">"</span><span style="color: #800000;">ename</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">new</span><span style="color: #000000;"> BsonDocument
- {
- { </span><span style="color: #800000;">"</span><span style="color: #800000;">firstname</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">jiu</span><span style="color: #800000;">"</span><span style="color: #000000;"> },
- { </span><span style="color: #800000;">"</span><span style="color: #800000;">lastname</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">wu</span><span style="color: #800000;">"</span><span style="color: #000000;"> }
- }
- }
- };
- </span><span style="color: #008000;">//</span><span style="color: #008000;">InsertOne()添加单条docment</span>
- <span style="color: #000000;"> collection.InsertOne(doc);
- }
- }</span>
执行完成后,查看在NoSQLBooster中查询,看到已经添加成功了:
我们也可以是用 collection.InsertMany(IEnumerable<BsonDocument> docs) 来批量添加docment,这里就不再演示了。
查找name=“吴九"的记录
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">Fileter用于过滤,如查询name = 吴九的第一条记录</span>
- <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #008000;">//</span><span style="color: #008000;">Find(filter)进行查询</span>
- <span style="color: #0000ff;">var</span> doc = collection.Find(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">吴九</span><span style="color: #800000;">"</span><span style="color: #000000;">)).FirstOrDefault();
- Console.WriteLine(doc);</span>
查询结果如下:
如果我们想查询所有的记录,可以设置过滤器为空,代码为: var docs = collection.Find(filter.Empty).ToList();
查询年龄大于25且小于28的记录
- <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">查询25<age<28的记录</span>
- <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Gt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>) & filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">28</span><span style="color: #000000;">)).ToList();
- docs.ForEach(d </span>=> Console.WriteLine(d));
查询结果如下:
查询年龄小于25或年龄大于28的记录
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">查询年龄小于25或年龄大于28的记录</span>
- <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>) | filter.Gt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">28</span><span style="color: #000000;">)).ToList();
- docs.ForEach(d </span>=> Console.WriteLine(d));
查询结果如下:
查询包含address字段的所有记录
- <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">查询存在address字段的记录</span>
- <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Exists(<span style="color: #800000;">"</span><span style="color: #800000;">address</span><span style="color: #800000;">"</span><span style="color: #000000;">)).ToList();
- docs.ForEach(d </span>=> Console.WriteLine(d));
查询结果如下:
查询年龄小于26岁的记录,并按年龄倒序排列
- <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">查询age<26的记录,按年龄倒序排列</span>
- <span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> sort = Builders<BsonDocument><span style="color: #000000;">.Sort;
- </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">26</span>))<span style="color: #008000;">//</span><span style="color: #008000;">过滤</span>
- .Sort(sort.Descending(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>)).ToList();<span style="color: #008000;">//</span><span style="color: #008000;">按age倒序</span>
- docs.ForEach(d => Console.WriteLine(d));
查询结果如下:
MongoDB查询会默认返回_id字段,如果要不返回_id字段可以使用Exclude("_id")将其排除。
- <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">查询age<26的记录 包含name age 排除 _id</span>
- <span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
- </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> docs = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">26</span>))<span style="color: #008000;">//</span><span style="color: #008000;">过滤</span>
- .Project(project.Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>)<span style="color: #008000;">//</span><span style="color: #008000;">包含name</span>
- .Include(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>)<span style="color: #008000;">//</span><span style="color: #008000;">包含age</span>
- .Exclude(<span style="color: #800000;">"</span><span style="color: #800000;">_id</span><span style="color: #800000;">"</span>)<span style="color: #008000;">//</span><span style="color: #008000;">不包含_id</span>
- <span style="color: #000000;"> ).ToList();
- docs.ForEach(d </span>=> Console.WriteLine(d));
查询结果:
修改符合过滤条件的第一条记录,例子:将张三的年龄改成18岁
- <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> update = Builders<BsonDocument><span style="color: #000000;">.Update;
- </span><span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
- </span><span style="color: #008000;">//</span><span style="color: #008000;">将张三的年龄改成18</span>
- collection.UpdateOne(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span>), update.Set(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">18</span><span style="color: #000000;">));
- </span><span style="color: #008000;">//</span><span style="color: #008000;">查询张三的记录</span>
- <span style="color: #0000ff;">var</span> doc = collection.Find(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span><span style="color: #000000;">))
- .Project(project.Include(</span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span><span style="color: #000000;">))
- .FirstOrDefault();
- Console.WriteLine(doc);</span>
执行结果:
UpdateMany会修改所有符合过滤条件的记录,例子:将所有年龄小于25的记录标记为young(如果没有mark字段会自动添加)
- <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> update = Builders<BsonDocument><span style="color: #000000;">.Update;
- </span><span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
- </span><span style="color: #008000;">//</span><span style="color: #008000;">将张三的年龄改成18</span>
- collection.UpdateOne(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span>), update.Set(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">18</span><span style="color: #000000;">));
- </span><span style="color: #008000;">//</span><span style="color: #008000;">查询张三的记录</span>
- <span style="color: #0000ff;">var</span> doc = collection.Find(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span><span style="color: #000000;">))
- .Project(project.Include(</span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span><span style="color: #000000;">))
- .FirstOrDefault();
- Console.WriteLine(doc);</span>
查询结果如下,可以看到age<25的记录的mark字段值为young:
删除操作比较简单,DeleteOne用于删除符合过滤条件的第一条记录,DeleteMany用于删除所有符合过滤条件的记录。
删除名字为张三的记录
- <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
- </span><span style="color: #008000;">//</span><span style="color: #008000;">删除名字为张三的记录</span>
- collection.DeleteOne(filter.Eq(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span><span style="color: #000000;">));
- </span><span style="color: #0000ff;">var</span> docs =<span style="color: #000000;"> collection.Find(filter.Empty)
- .Project(project.Include(</span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">mark</span><span style="color: #800000;">"</span><span style="color: #000000;">))
- .ToList();
- docs.ForEach(d </span>=> Console.WriteLine(d));
执行结果如下,我们看到张三的记录已经被删除了:
删除所有年龄大于25岁的记录
- <span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<BsonDocument>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #0000ff;">var</span> filter = Builders<BsonDocument><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> project = Builders<BsonDocument><span style="color: #000000;">.Projection;
- </span><span style="color: #008000;">//</span><span style="color: #008000;">删除age>25的记录</span>
- DeleteResult result = collection.DeleteMany(filter.Gt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span><span style="color: #000000;">));
- Console.WriteLine($</span><span style="color: #800000;">"</span><span style="color: #800000;">一共删除了{result.DeletedCount}条记录</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #0000ff;">var</span> docs =<span style="color: #000000;"> collection.Find(filter.Empty)
- .Project(project.Include(</span><span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span>).Include(<span style="color: #800000;">"</span><span style="color: #800000;">mark</span><span style="color: #800000;">"</span><span style="color: #000000;">))
- .ToList();
- docs.ForEach(d </span>=> Console.WriteLine(d));
执行结果如下,所有年龄大于25岁的记录都被删除了:
有时候我们要让查询的结果映射到我们的实体类中去,实现这个需求也十分简单,mongoDB支持自动映射,直接使用泛型即可,看一个(清空数据db.userinfos.remove({}),从新初始化js脚本):
- <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
- {
- </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
- {
- </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<Userinfo>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #0000ff;">var</span> filter = Builders<Userinfo><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> sort = Builders<Userinfo><span style="color: #000000;">.Sort;
- List</span><Userinfo> userinfos = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>)) <span style="color: #008000;">//</span><span style="color: #008000;">查询年龄小于25岁的记录</span>
- .Sort(sort.Descending(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>)) <span style="color: #008000;">//</span><span style="color: #008000;">按年龄进行倒序</span>
- <span style="color: #000000;"> .ToList();
- </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历结果</span>
- userinfos.ForEach(u => Console.WriteLine($<span style="color: #800000;">"</span><span style="color: #800000;">姓名:{u.name},年龄:{u.age},英文名:{u.ename.firstname} {u.ename.lastname}</span><span style="color: #800000;">"</span><span style="color: #000000;">));
- Console.ReadKey();
- }
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
- <span style="color: #808080;">///</span><span style="color: #008000;"> 用户类
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Userinfo
- {
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> _id { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">id</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> name { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">姓名</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> age { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">年龄</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> level { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">等级</span>
- <span style="color: #0000ff;">public</span> Ename ename { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">英文名</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span>[] roles { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">角色</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> address { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">地址</span>
- <span style="color: #000000;"> }
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
- <span style="color: #808080;">///</span><span style="color: #008000;"> 英文名
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Ename
- {
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> firstname { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> lastname { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
- }</span>
执行结果如下:
上边的例子仅仅用了基本的自动化映射,使用基本的自动化映射时:类和Bson中的字段必须严格一致(_id除外,可以自动映射到_id/id/Id),且Bson中的每一个字段在实体类中都必须有一个对应的字段,不然就会抛出异常,这就造成我们可能要写一个非常庞大的实体类,而且类中的字段命名也要严格和Bson中的字段一致。这些限制对我们开发来说是不能接受的,这里我们采用mongoDriver中的一些属性改进一下上边的代码,如下:
- <span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
- {
- </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
- {
- </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> collection = mydb.GetCollection<Userinfo>(<span style="color: #800000;">"</span><span style="color: #800000;">userinfos</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #0000ff;">var</span> filter = Builders<Userinfo><span style="color: #000000;">.Filter;
- </span><span style="color: #0000ff;">var</span> sort = Builders<Userinfo><span style="color: #000000;">.Sort;
- List</span><Userinfo> userinfos = collection.Find(filter.Lt(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>, <span style="color: #800080;">25</span>)) <span style="color: #008000;">//</span><span style="color: #008000;">查询年龄小于25岁的记录</span>
- .Sort(sort.Descending(<span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>)) <span style="color: #008000;">//</span><span style="color: #008000;">按年龄进行倒序</span>
- <span style="color: #000000;"> .ToList();
- </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历结果</span>
- userinfos.ForEach(u =><span style="color: #000000;">
- {
- Console.WriteLine($</span><span style="color: #800000;">"</span><span style="color: #800000;">编号:{u.userId},姓名:{u.name},年龄:{u.age},英文名:{u.ename?.ming} {u.ename?.xing},性别:{u.gender}</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- Console.WriteLine($</span><span style="color: #800000;">"</span><span style="color: #800000;">其他属性:{u.otherprops}</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- Console.WriteLine();
- });
- Console.ReadKey();
- }
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
- <span style="color: #808080;">///</span><span style="color: #008000;"> 用户类
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary
- </span><span style="color: #008000;"> //[BsonIgnoreExtraElements]</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Userinfo
- {
- [BsonId]
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> userId { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">id</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> name { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">姓名</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> age { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">年龄</span>
- <span style="color: #0000ff;">public</span> Ename ename { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">英文名</span>
- [BsonDefaultValue(<span style="color: #800000;">‘</span><span style="color: #800000;">男</span><span style="color: #800000;">‘</span><span style="color: #000000;">)]
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">char</span> gender { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
- [BsonIgnore]
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> nickname { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">昵称</span>
- <span style="color: #000000;"> [BsonExtraElements]
- </span><span style="color: #0000ff;">public</span> BsonDocument otherprops { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">其他属性</span>
- <span style="color: #000000;"> }
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
- <span style="color: #808080;">///</span><span style="color: #008000;"> 英文名
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Ename
- {
- [BsonElement(</span><span style="color: #800000;">"</span><span style="color: #800000;">firstname</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> ming { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
- [BsonElement(</span><span style="color: #800000;">"</span><span style="color: #800000;">lastname</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> xing { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
- }</span>
执行结果如下:
这里用到了几个常用的属性,作用如下:
BsonId修饰的字段对应BsonDocument中的_id;
BsonDefaultValue(value)用于指定默认值;
BsonIgnore表示不映射,即使BsonDocument中包含该字段也不会赋值给属性;
BsonExtraElements修饰的字段用于存储没有映射到类中的其他属性;
BsonElement可以指定修饰的属性映射到BsonDocument中的哪个字段,
还有一些其他的属性,具体可以参考官方文档。
MongoDB的驱动支持Linq查询,用法十分简单,引用 usingMongoDB.Driver.Linq; 后,使用 collection.AsQueryable() 获取IMongoQueryable<T>实例,然后就可以使用Linq对这个IMongoQueryable<T>进行查询了。
使用过EF的小伙伴应该都了解IQueryable+Linq的查询默认不是将整个结果集立即加载到内存中,而是延迟加载的,即只有使用结果时(如 在执行First,Last,Single,Count,ToList等)才会将Linq转换成Sql,在数据库中执行查询操作。类似的,在使用IMongoQueryable<T>+Linq进行查询时默认也是延迟加载的,在需要使用查询结果时,驱动会将Linq转换成聚合管道命令(aggregation pipeline),如Linq中的Where被转换成$watch,Join转换为$lookup,Skip和Take转换为$skip和$limit等等,然后在Mongodb中执行这些管道命令获取结果,看一个栗子我们就会轻松地掌握了。
首先添加一些测试数据:
- <span style="color: #008000;">//</span><span style="color: #008000;">添加学生数据</span>
- <span style="color: #000000;">db.students.insertMany([
- {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span>:<span style="color: #800080;">1</span>, <span style="color: #800000;">"</span><span style="color: #800000;">stuName</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">jack</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>:<span style="color: #800080;">23</span>, <span style="color: #800000;">"</span><span style="color: #800000;">classNo</span><span style="color: #800000;">"</span>:<span style="color: #800080;">1</span><span style="color: #000000;">},
- {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span>:<span style="color: #800080;">2</span>, <span style="color: #800000;">"</span><span style="color: #800000;">stuName</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">tom</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>:<span style="color: #800080;">20</span>, <span style="color: #800000;">"</span><span style="color: #800000;">classNo</span><span style="color: #800000;">"</span>:<span style="color: #800080;">2</span><span style="color: #000000;">},
- {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span>:<span style="color: #800080;">3</span>, <span style="color: #800000;">"</span><span style="color: #800000;">stuName</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">hanmeimei</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>:<span style="color: #800080;">22</span>, <span style="color: #800000;">"</span><span style="color: #800000;">classNo</span><span style="color: #800000;">"</span>:<span style="color: #800080;">1</span><span style="color: #000000;">},
- {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span>:<span style="color: #800080;">4</span>, <span style="color: #800000;">"</span><span style="color: #800000;">stuName</span><span style="color: #800000;">"</span>:<span style="color: #800000;">"</span><span style="color: #800000;">lilei</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">age</span><span style="color: #800000;">"</span>:<span style="color: #800080;">24</span>, <span style="color: #800000;">"</span><span style="color: #800000;">classNo</span><span style="color: #800000;">"</span>:<span style="color: #800080;">2</span><span style="color: #000000;">}
- ])
- </span><span style="color: #008000;">//</span><span style="color: #008000;">添加班级数据</span>
- <span style="color: #000000;">db.classes.insertMany([
- {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span> : <span style="color: #800080;">1</span>,<span style="color: #800000;">"</span><span style="color: #800000;">clsName</span><span style="color: #800000;">"</span> : <span style="color: #800000;">"</span><span style="color: #800000;">A班</span><span style="color: #800000;">"</span><span style="color: #000000;">},
- {</span><span style="color: #800000;">"</span><span style="color: #800000;">no</span><span style="color: #800000;">"</span> : <span style="color: #800080;">2</span>,<span style="color: #800000;">"</span><span style="color: #800000;">clsName</span><span style="color: #800000;">"</span> : <span style="color: #800000;">"</span><span style="color: #800000;">B班</span><span style="color: #800000;">"</span><span style="color: #000000;">}
- ])</span>
这里查找了两组数据:①基本查询:查找年龄大于22岁的学生;②连接查询:查询各个学生的学号、姓名、班级名。例子比较简单,直接看代码吧
- <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ConsoleAppCore
- {
- </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System;
- </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> MongoDB.Bson;
- </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> MongoDB.Bson.Serialization.Attributes;
- </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> MongoDB.Driver;
- </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> MongoDB.Driver.Linq;
- </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Collections.Generic;
- </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Program
- {
- </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
- {
- </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库</span>
- <span style="color: #0000ff;">var</span> client = <span style="color: #0000ff;">new</span> MongoClient(<span style="color: #800000;">"</span><span style="color: #800000;">mongodb://root:root@192.168.100.3:27017/</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取database</span>
- <span style="color: #0000ff;">var</span> mydb = client.GetDatabase(<span style="color: #800000;">"</span><span style="color: #800000;">myDb</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">获取collection</span>
- <span style="color: #0000ff;">var</span> stuCollection = mydb.GetCollection<Student>(<span style="color: #800000;">"</span><span style="color: #800000;">students</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #0000ff;">var</span> clsCollection = mydb.GetCollection<Classx>(<span style="color: #800000;">"</span><span style="color: #800000;">classes</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">查找年龄大于22的学生</span>
- Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">-------------查找年龄大于22的学生列表--------------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">1.query语法</span>
- List<Student> stuList1 = (<span style="color: #0000ff;">from</span> stu <span style="color: #0000ff;">in</span><span style="color: #000000;"> stuCollection.AsQueryable()
- </span><span style="color: #0000ff;">where</span> stu.age > <span style="color: #800080;">22</span>
- <span style="color: #0000ff;">select</span><span style="color: #000000;"> stu).ToList();
- </span><span style="color: #008000;">//</span><span style="color: #008000;">2.点语法</span>
- List<Student> stuList2 = stuCollection.AsQueryable().Where(s => s.age > <span style="color: #800080;">22</span><span style="color: #000000;">).ToList();
- stuList1.ForEach(stu </span>=> Console.WriteLine($<span style="color: #800000;">"</span><span style="color: #800000;">姓名:{stu?.stuName}, 年龄:{stu?.age}</span><span style="color: #800000;">"</span><span style="color: #000000;">));
- Console.WriteLine();
- </span><span style="color: #008000;">//</span><span style="color: #008000;">表连接查询,查询各个学生的班级名</span>
- Console.WriteLine(<span style="color: #800000;">"</span><span style="color: #800000;">-------------表连接,查询学生的班级名----------------</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- </span><span style="color: #008000;">//</span><span style="color: #008000;">1.query语法</span>
- <span style="color: #0000ff;">var</span> result1 = <span style="color: #0000ff;">from</span> stu <span style="color: #0000ff;">in</span><span style="color: #000000;"> stuCollection.AsQueryable()
- join cls </span><span style="color: #0000ff;">in</span><span style="color: #000000;"> clsCollection.AsQueryable()
- on stu.classNo equals cls.no
- </span><span style="color: #0000ff;">select</span> <span style="color: #0000ff;">new</span> { stuno =<span style="color: #000000;"> stu.no, stu.stuName, cls.clsName };
- </span><span style="color: #008000;">//</span><span style="color: #008000;">2.点语法</span>
- <span style="color: #0000ff;">var</span> result2 =<span style="color: #000000;"> stuCollection.AsQueryable().Join(
- clsCollection.AsQueryable(),
- stu </span>=><span style="color: #000000;"> stu.classNo,
- cls </span>=><span style="color: #000000;"> cls.no,
- (stu, cls) </span>=> <span style="color: #0000ff;">new</span> { stuno =<span style="color: #000000;"> stu.no, stu.stuName, cls.clsName }
- );
- </span><span style="color: #008000;">//</span><span style="color: #008000;">遍历结果</span>
- <span style="color: #0000ff;">foreach</span> (<span style="color: #0000ff;">var</span> item <span style="color: #0000ff;">in</span><span style="color: #000000;"> result1)
- {
- Console.WriteLine($</span><span style="color: #800000;">"</span><span style="color: #800000;">学号:{item.stuno}, 姓名:{item.stuName}, 班级:{item.clsName}</span><span style="color: #800000;">"</span><span style="color: #000000;">);
- }
- Console.ReadKey();
- }
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
- <span style="color: #808080;">///</span><span style="color: #008000;"> 学生类
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary
- </span><span style="color: #008000;"> public class Student</span>
- <span style="color: #000000;"> {
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> no { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">学号</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> stuName { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">姓名</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> age { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">年龄</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> classNo { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">班级编号</span>
- <span style="color: #000000;"> [BsonExtraElements]
- </span><span style="color: #0000ff;">public</span> BsonDocument others { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
- }
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
- <span style="color: #808080;">///</span><span style="color: #008000;"> 班级类
- </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Classx
- {
- </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> no { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">班级编号</span>
- <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> clsName { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }<span style="color: #008000;">//</span><span style="color: #008000;">班级名</span>
- <span style="color: #000000;"> [BsonExtraElements]
- </span><span style="color: #0000ff;">public</span> BsonDocument others { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
- }
- }
- }</span>
执行结果如下:
.net Core MongoDB用法演示
标签:查询 简单例子 clu nosql ESS mamicode 命名 nbsp project