用C#操作Mongodb(c#mongodb驱动) -3
- UID
- 1066743
|
用C#操作Mongodb(c#mongodb驱动) -3
插入成功
查询数据:
在已建的类Student代码里添加类结构:Student_Query
添加后类Student.cs的代码为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace mymongotest
{
class Student_Insert
{
public string 姓名{get;set;}
public string 学号{get;set;}
public Information[] 个人信息{get;set;}
}
class Information
{
public string Name { get; set; }
public string Value { get; set; }
}
class Student_Query
{
public Object _id { get; set; }
public string 姓名 { get; set; }
public string 学号 { get; set; }
public Information[] 个人信息 { get; set; }
}
}
情景一:通过索引值取数据----通常用于取上一条,下一条数据。(用到类结构)
在窗体下声明集合coll和浮标cucor:
MongoCollection<Student_Query> coll;
MongoCursor<Student_Query> cursor;
声明位置如图:
查询按钮的代码:
private void button3_Click(object sender, EventArgs e)
{
MongoClient client;
MongoServer server;
MongoDatabase database;
var conStr = "mongodb://192.168.0.188";
client = new MongoClient(conStr);
server = client.GetServer();
database = server.GetDatabase("test");
coll = database.GetCollection< Student_Query>("student");
cursor = coll.FindAll();
var ex = cursor.ElementAt< Student_Query>(0);//取第一个数据
string sResult = "";
sResult = string.Format("{0}{1}{2}{3}{4}", ex.姓名, ex.学号, ex.个人信息[0].Value, ex.个人信息[1].Value, ex.个人信息[2].Value);
MessageBox.Show(sResult);
}
结果:
情景二:用条件查询
情景二方法一:(用类结构)
查询按钮代码如下:
MongoClient client;
MongoServer server;
MongoDatabase database;
var conStr = "mongodb://192.168.0.188";
client = new MongoClient(conStr);
server = client.GetServer();
database = server.GetDatabase("test");
coll = database.GetCollection<Student_Query>("student");
// var bs = coll.FindOne(Query.EQ("_id", new ObjectId("512c14c211f9d518809d8d0c")));
var bs = coll.FindOne(Query.EQ("姓名", "赵云"));
string sResult = "";
sResult = string.Format("{0}{1}{2}{3}{4}", bs.姓名, bs.学号, bs.个人信息[0].Value, bs.个人信息[1].Value, bs.个人信息[2].Value);
MessageBox.Show(sResult);
结果:
情景二方法二:(不用类结构)
查询按钮代码如下:
MongoClient client;
MongoServer server;
MongoDatabase database;
var conStr = "mongodb://192.168.0.188";
client = new MongoClient(conStr);
server = client.GetServer();
database = server.GetDatabase("test");
var coll = database.GetCollection("student");
// var bs = coll.FindOne(Query.EQ("_id", new ObjectId("512c14c211f9d518809d8d0c")));
var bs = coll.FindOne(Query.EQ("姓名", "赵云"));
string sResult = "";
sResult = string.Format("{0}{1}{2}{3}{4}", bs.AsBsonDocument["姓名"].ToString(), bs.AsBsonDocument["学号"].ToString(), bs.AsBsonDocument["个人信息"].AsBsonArray[0].AsBsonDocument["Value"].ToString(), bs.AsBsonDocument["个人信息"].AsBsonArray[1].AsBsonDocument["Value"].ToString(), bs.AsBsonDocument["个人信息"].AsBsonArray[2].AsBsonDocument["Value"].ToString());
MessageBox.Show(sResult);
结果:
查询常用语句:
求文档长度
var bs = coll.FindAll();
string i= bs.Count().ToString();
求文档中数组长度
var bs = coll.FindOne(Query.EQ("姓名", "赵云"));
string i = bs.AsBsonDocument["个人信息"].AsBsonArray.Count().ToString();
文档中是否包含某个元素:
if (bs.AsBsonDocument.Contains("姓名"))
遍历一个文档中的所有属性:
var bs = coll_fieldConnect.FindOne(Query.EQ("id", process_id));
StringBuilder sb = new StringBuilder();
foreach (BsonElement d in bs)
{
sb.AppendFormat("{0}:\t{1}\n", d.Name, d.Value);
}
richTextBox1.Text = sb.ToString(); |
|
|
|
|
|