Advanced Interdisciplinary Projects Lab(AIPLab) 討論區

Please login or register.

請輸入帳號, 密碼以及預計登入時間

新聞:

[慶賀]恭喜亞大獲《泰晤士報》亞洲最佳大學排名第83名,國內排名第十名-20170201

作者 主題: MongoDB Driver C# Quick Tour  (閱讀 386 次)

admin

  • 管理員
  • Hero Member
  • *****
  • 文章: 1687
    • 檢視個人資料
MongoDB Driver C# Quick Tour
« 於: 六月 11, 2016, 05:48:11 pm »
http://mongodb.github.io/mongo-csharp-driver/2.2/getting_started/quick_tour/
Make a connection
程式碼: [Select]
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("foo");
var collection = database.GetCollection<BsonDocument>("bar");

Insert a Document
程式碼: [Select]
var document = new BsonDocument
{
    { "name", "MongoDB" },
    { "type", "Database" },
    { "count", 1 },
};
collection.InsertOne(document);
Insert Multiple Documents
程式碼: [Select]
collection.InsertMany(documents);Get a Single Document with a Filter
程式碼: [Select]

var filter = Builders<BsonDocument>.Filter.Eq("i", 71);
var filter = Builders<BsonDocument>.Filter.Gt("i", 50);
var filterBuilder = Builders<BsonDocument>.Filter;
var filter = filterBuilder.Gt("i", 50) & filterBuilder.Lte("i", 100);
var cursor = collection.Find(filter).ToCursor();
foreach (var document in cursor.ToEnumerable())
{
    Console.WriteLine(document);   
}
Updating Documents
程式碼: [Select]
var filter = Builders<BsonDocument>.Filter.Lt("i", 100);
var update = Builders<BsonDocument>.Update.Inc("i", 100);
var result = collection.UpdateOne(filter, update);
if (result.IsModifiedCountAvailable)
{
    Console.WriteLine(result.ModifiedCount);
}
已記錄
 

SimplePortal Classic 2.0.5