using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MongoDB.Driver;
using MongoDB.Bson;
namespace mangodb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_import_Click(object sender, EventArgs e)
{
//MongoDB服务器连接串
string connectionString = "mongodb://localhost:27017";
MongoServer server = MongoServer.Create(connectionString);
//连接到 mongodb_c_demo 数据库
MongoDatabase db = server.GetDatabase("mongodb_c_demo");
//获取集合 fruit
MongoCollection collection = db.GetCollection("websiteUrl");
inserData(collection);
//查询所有
//collection.FindAllAs(Type.get);
//MongoCursor cur = collection.FindAllAs(Type.d
//while(cur. ()) {
// System.out.println(cur.next());
//}
//以上代码完成的就是向fruit表中插入2条数据,用mysql的语法解释即
//insert into mongodb_c_demo.fruit (name, color)
//values ('apple', 'red'), ('banana', 'yellow');
}
private void btn_sel_Click(object sender, EventArgs e)
{
//MongoDB服务器连接串
string connectionString = "mongodb://localhost:27017";
MongoServer server = MongoServer.Create(connectionString);
//连接到 mongodb_c_demo 数据库
MongoDatabase db = server.GetDatabase("mongodb_c_demo");
//获取集合 fruit
MongoCollection collection = db.GetCollection("websiteUrl");
selectAll(collection);
}
private void btn_del_Click(object sender, EventArgs e)
{
//MongoDB服务器连接串
string connectionString = "mongodb://localhost:27017";
MongoServer server = MongoServer.Create(connectionString);
//连接到 mongodb_c_demo 数据库
MongoDatabase db = server.GetDatabase("mongodb_c_demo");
//获取集合 fruit
MongoCollection collection = db.GetCollection("websiteUrl");
delData(collection);
}
private void btn_update_Click(object sender, EventArgs e)
{
//MongoDB服务器连接串
string connectionString = "mongodb://localhost:27017";
MongoServer server = MongoServer.Create(connectionString);
//连接到 mongodb_c_demo 数据库
MongoDatabase db = server.GetDatabase("mongodb_c_demo");
//获取集合 fruit
MongoCollection collection = db.GetCollection("websiteUrl");
update(collection);
}
private void btn_docToDatabase_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="collection"></param>
private void inserData(MongoCollection collection)
{
//创建对象 fruit_1
BsonDocument fruit_1 = new BsonDocument
{
{ "_id", 10001 },
{ "website", "http://www.my400800.cn
" },
{ "name", "400电话" },
{ "count", 10 },
{ "time", DateTime.Now }
};
//创建对象 fruit_2
BsonDocument fruit_2 = new BsonDocument
{
{ "_id", 10002 },
{ "website", "http://www.hrxc.net
" },
{ "name", "华仁信诚" },
{ "count", 20 },
{ "time", DateTime.Now }
};
//将对象 fruit_1 放到集合 fruit 中
collection.Insert(fruit_1);
//将对象 fruit_2 放到集合 fruit 中
collection.Insert(fruit_2);
}
/// <summary>
/// 数据检索
/// </summary>
/// <param name="coll"></param>
private void selectAll(MongoCollection coll)
{
News firstNews = coll.FindOneAs<News>();
//查找第一个文档
QueryDocument query = new QueryDocument();
//定义查询文档
query.Add("_id", 10001);
query.Add("count", 10);
MongoCursor<News> qNews = coll.FindAs<News>(query);
foreach (News oneRecGouRu in qNews)
{
Console.WriteLine(oneRecGouRu.name.ToString()+"\r\n");
}
BsonDocument bd = new BsonDocument();
//定义查询文档 count>2 and count<=4
bd.Add("$gt", 2);
bd.Add("$lte", 10);
QueryDocument query_a = new QueryDocument();
query_a.Add("count", bd);
FieldsDocument fd = new FieldsDocument();
fd.Add("_id", 0);
fd.Add("count", 1);
fd.Add("time", 1);
MongoCursor<News> mNewss = coll.FindAs<News>(query_a).SetFields(fd);
//只返回count和time
var time = BsonDateTime.Create(Convert.ToDateTime("2011/09/05 23:26:00"));
BsonDocument db_t = new BsonDocument();
db_t.Add("$gt", time);
QueryDocument qd_3 = new QueryDocument();
qd_3.Add("time", db_t);
MongoCursor<News> mNkews = coll.FindAs<News>(qd_3);
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="coll"></param>
private void delData(MongoCollection coll)
{
QueryDocument query_a = new QueryDocument();
query_a.Add("count", 10);
coll.Remove(query_a);
}
private void update(MongoCollection coll)
{
News customer = new News();
customer.name = "呼叫中心";
customer.website = "http://www.hrxc.net";
customer.time = DateTime.Now;
//查找第一个文档
QueryDocument query = new QueryDocument();
IList<BsonElement> elements = new List<BsonElement>();
//定义查询文档
query.Add("_id", 10001);
query.Add("count", 10);
BsonDocument doc = BsonExtensionMethods.ToBsonDocument(customer);
foreach (var item in doc.Elements) {
if (item.Value.IsBsonNull || item.Name=="_id")
{
elements.Add(item);
}
}
foreach (var el in elements) {
doc.RemoveElement(el);
}
var update = new UpdateDocument(doc);
coll.Update(query, update);
//coll.Update(customer,(xw=>xw.name==''));
//coll.Update(customer, (x => x. == customer.CustomerID));
//coll.Update(null,IMongoUpdate.
}
}
public class News
{
public int _id { get; set; }
public string website { get; set; }
public string name { get; set; }
public int count { get; set; }
public DateTime time { get; set; }
}
}