標籤:rom get last lin table 直接 sar connect fun
驅動
- 下載
https://github.com/mongodb/mongo-csharp-driver/downloads
項目地址:
https://github.com/mongodb/mongo-csharp-driver
1.10 使用參考:
http://mongodb.github.io/mongo-csharp-driver/1.10/
1.10 API
http://api.mongodb.org/csharp/1.10/
官方推薦使用Nuget進行安裝。但我搜尋時出來東西太多了,分不清,故直接下載。
要注意的是,2.0 和1.10支援的環境不同。
C#/.NET Driver Version |
MongoDB 2.4 |
MongoDB 2.6 |
MongoDB 3.0 |
Version 2.0 |
? |
? |
? |
Version 1.10 |
? |
? |
? |
Driver Version |
.NET 3.5 |
.NET 4.0 |
.NET 4.5 |
Mono 2.10 |
Mono 3.x |
Version 2.0 |
|
|
? |
|
? |
Version 1.10 |
? |
? |
? |
? |
? |
我這裡下載1.10版本,framework 4.0環境。
使用串連
using MongoDB.Bson;using MongoDB.Driver;var client = new MongoClient("mongodb://localhost:27017");var server = client.GetServer();var database = server.GetDatabase("foo");var collection = database.GetCollection<BsonDocument>("bar");await collection.InsertOneAsync(new BsonDocument("Name", "Jack"));var list = await collection.Find(new BsonDocument("Name", "Jack")) .ToListAsync();foreach(var document in list){ Console.WriteLine(document["Name"]);}
Document是實體類
using MongoDB.Bson;using MongoDB.Driver;public class Person{ public ObjectId Id { get; set; } public string Name { get; set; }}var client = new MongoClient("mongodb://localhost:27017");var server = client.GetServer();var database = server.GetDatabase("foo");var collection = database.GetCollection<Person>("bar");await collection.InsertOneAsync(new Person { Name = "Jack" });var list = await collection.Find(x => x.Name == "Jack") .ToListAsync();foreach(var person in list){ Console.WriteLine(person.Name);}
實體類給下面的代碼使用
public class Entity{ public ObjectId Id { get; set; } public string Name { get; set; }}
插入
var entity = new Entity { Name = "Tom" };collection.Insert(entity);var id = entity.Id; // Insert will set the Id if necessary (as it was in this example)
尋找
var query = Query<Entity>.EQ(e => e.Id, id);var entity = collection.FindOne(query);// var entity = collection.FindOneByIdAs<Entity>(id);/* 定義一個查詢:查詢stdid=1的文檔FindOneArgs args = new FindOneArgs { Query = Query.EQ("stdid", 1),//查詢stdid field等於1的document。};//查詢var std = collection.FindOneAs<Student>(args);*//* 查詢多條 IMongoQuery query = Query.GTE("stdid",2); var result=collection.FindAs<Student>(Query.GTE("stdid",2)); foreach (var each in result) { Console.WriteLine(each.stdName); }*/
儲存
entity.Name = "Dick";collection.Save(entity);
更新
var query = Query<Entity>.EQ(e => e.Id, id);var update = Update<Entity>.Set(e => e.Name, "Harry"); // update modifierscollection.Update(query, update);
刪除
var query = Query<Entity>.EQ(e => e.Id, id);collection.Remove(query);
這是一個完整的樣本:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using MongoDB.Bson;using MongoDB.Driver;using MongoDB.Driver.Builders;namespace ConsoleApplication1{ public class Entity { public ObjectId Id { get; set; } public string Name { get; set; } } class Program { static void Main(string[] args) { var connectionString = "mongodb://localhost"; var client = new MongoClient(connectionString); var server = client.GetServer(); var database = server.GetDatabase("test"); var collection = database.GetCollection<Entity>("entities"); var entity = new Entity { Name = "Tom" }; collection.Insert(entity); var id = entity.Id; var query = Query<Entity>.EQ(e => e.Id, id); entity = collection.FindOne(query); entity.Name = "Dick"; collection.Save(entity); var update = Update<Entity>.Set(e => e.Name, "Harry"); collection.Update(query, update); collection.Remove(query); } }}
參考:http://mongodb.github.io/mongo-csharp-driver/1.10/getting_started/
LinQ查詢
C# driver 1.8版本開始支援Linq查詢。
var linquery = from e in collection.AsQueryable<SY.Model.User>() //where e.age> 22 select e;linquery=linquery.Where(c=>c.Name=="張三");int count=linquery.Count();
參考:http://mongodb.github.io/mongo-csharp-driver/1.10/linq/
Document查詢方式
未測試,參考地址記錄在這裡。
//Document docName = new Document { { "欄位名1", "輸入值1" }, { "欄位名2", "輸入值2" } };/// <summary>/// 根據姓名擷取使用者資訊/// </summary>/// <param name="mUserInfo">使用者Model類</param>/// <returns>使用者泛型集合</returns>public List<UserInfo> GetUserByName(UserInfo mUserInfo){ List<UserInfo> lsUser = new List<UserInfo>(); using (Mongo mongo = new Mongo("mongodb://localhost")) { MongoDatabase mongoDatabase = mongo.GetDatabase("UserInfo") as MongoDatabase; MongoCollection<Document> mongoCollection = mongoDatabase.GetCollection<Document>("myCollection") as MongoCollection<Document>; mongo.Connect(); Document docName = new Document { { "FirstName", "aaa" }, { "LastName", "bbb" } }; MongoDB.ICursor<Document> users = mongoCollection.Find(docName); foreach (Document user in users.Documents) { UserInfo mUser = new UserInfo(); mUser.FirstName = user["FirstName"].ToString(); mUser.LastName = user["LastName"].ToString(); mUser.CorporationName = user["CorporationName"].ToString(); mUser.Phone = user["Phone"].ToString(); mUser.Email = user["Email"].ToString(); mUser.UserType = user["UserType"].ToString(); lsUser.Add(mUser); } } return lsUser;}
http://weishangxue.blog.163.com/blog/static/21575188201181633811102/
http://mikaelkoskinen.net/post/mongodb-aggregation-framework-examples-in-c
MongoDB 學習筆記四 C#調用MongoDB