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; }
}
}