- class Program
- {
- const string CollectionName = "video";
- static void Main(string[] args)
- {
- // remove the demo collection then recreate later
- db.GetCollection<Video>(CollectionName).Database.DropCollection(CollectionName);
-
-
- var videos = new List<Video>
- {
- new Video { Title="The Perfect Developer",
- Category="SciFi", Minutes=118 },
- new Video { Title="Lost In Frankfurt am Main",
- Category="Horror", Minutes=122 },
- new Video { Title="The Infinite Standup",
- Category="Horror", Minutes=341 }
- };
-
-
- Console.WriteLine("Insert Videos ...");
-
-
- db.GetCollection<Video>(CollectionName).InsertMany(videos);
-
-
- Console.WriteLine("[After insert] All Videos : ");
- var all = db.GetCollection<Video>(CollectionName).Find(x=>x.Title != string.Empty).ToList();
- foreach (var v in all)
- {
- Console.WriteLine(v);
- }
-
-
- Console.WriteLine("Group By...");
-
-
- var groupby = db.GetCollection<Video>(CollectionName).Aggregate()
- .Group(x => x.Category, g => new {Name = g.Key, Count = g.Count(), TotalMinutes = g.Sum(x => x.Minutes)})
- .ToList();
- foreach (var v in groupby)
- {
- Console.WriteLine(v.Name + "," + v.Count + "," + v.TotalMinutes);
- }
-
-
-
-
- Console.WriteLine("Updating One [Title = The Perfect Developer]...");
-
-
- // updating title with "The perfect developer" video's 'title' and 'minute'
- db.GetCollection<Video>(CollectionName).FindOneAndUpdate(x=>x.Title == "The Perfect Developer",
- Builders<Video>.Update.Set(x=> x.Title , "A Perfect Developer [updated]")
- .AddToSet(x => x.Comments, "good video!")
- .AddToSet(x => x.Comments, "not bad"));
-
-
- Console.WriteLine("[After Updating One] All Videos : ");
- all = db.GetCollection<Video>(CollectionName).Find(x => x.Title != string.Empty).ToList();
- foreach (var v in all)
- {
- Console.WriteLine(v);
- }
-
-
- Console.WriteLine("Deleting One... [Minutes = 122]");
- db.GetCollection<Video>(CollectionName).DeleteOne(x => x.Minutes == 122);
- Console.WriteLine("[After Deleting One] All Videos : ");
- all = db.GetCollection<Video>(CollectionName).Find(x => x.Title != string.Empty).ToList();
- foreach (var v in all)
- {
- Console.WriteLine(v);
- }
-
-
- Console.Read();
- }
-
-
- private static IMongoDatabase db
- {
- get
- {
- var url = new MongoUrl(ConfigurationSettings.AppSettings["mongoUrl"]);
- var client = new MongoClient(url);
- return client.GetDatabase(url.DatabaseName);
- }
- }
- }
-
-
- [BsonIgnoreExtraElements]
- public class Video
- {
- public Video()
- {
- Comments = new List<string>();
- }
-
-
- [BsonId]
- [BsonRepresentation(BsonType.ObjectId)]
- public string Id { get; set; }
-
-
- public string Title { get; set; }
- public string Category { get; set; }
- public int Minutes { get; set; }
-
-
- public IList<string> Comments { get; set; }
-
-
- public override string ToString()
- {
- return string.Format("{0} - {1} - {2}", Title, Category, Minutes);
- }
- }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MongoDB.Driver;
using MongoDB.Bson;
namespace MongoDBTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Mongo();
}
public void Mongo()
{
//建立连接
var client = new MongoClient();
//建立数据库
var database = client.GetDatabase("TestDb");
//建立collection
var collection = database.GetCollection<BsonDocument>("foo");
var document = new BsonDocument
{
{"name","MongoDB"},
{"type","Database"},
{"count",1},
{"info",new BsonDocument{{"x",203},{"y",102}}}
};
//插入数据
collection.InsertOne(document);
var count = collection.Count(document);
Console.WriteLine(count);
//查询数据
var document1 = collection.Find(document);
Console.WriteLine(document1.ToString());
//更新数据
var filter = Builders<BsonDocument>.Filter.Eq("name", "MongoDB");
var update = Builders<BsonDocument>.Update.Set("name", "Ghazi");
collection.UpdateMany(filter, update);
//删除数据
var filter1 = Builders<BsonDocument>.Filter.Eq("count", 101);
collection.DeleteMany(filter1);
BsonDocument document2 = new BsonDocument();
document2.Add("name", "MongoDB");
document2.Add("type", "Database");
document2.Add("count", "1");
collection.InsertOne(document2);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MongoDB.Driver;
using MongoDB.Bson;
namespace MongoDBTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Mongo();
}
public void Mongo()
{
//建立连接
var client = new MongoClient();
//建立数据库
var database = client.GetDatabase("TestDb");
//建立collection
var collection = database.GetCollection<BsonDocument>("foo");
var document = new BsonDocument
{
{"name","MongoDB"},
{"type","Database"},
{"count",1},
{"info",new BsonDocument{{"x",203},{"y",102}}}
};
//插入数据
collection.InsertOne(document);
var count = collection.Count(document);
Console.WriteLine(count);
//查询数据
var document1 = collection.Find(document);
Console.WriteLine(document1.ToString());
//更新数据
var filter = Builders<BsonDocument>.Filter.Eq("name", "MongoDB");
var update = Builders<BsonDocument>.Update.Set("name", "Ghazi");
collection.UpdateMany(filter, update);
//删除数据
var filter1 = Builders<BsonDocument>.Filter.Eq("count", 101);
collection.DeleteMany(filter1);
BsonDocument document2 = new BsonDocument();
document2.Add("name", "MongoDB");
document2.Add("type", "Database");
document2.Add("count", "1");
collection.InsertOne(document2);
}
}
}