.Net Core中簡單使用MongoDB

江北、發表於2020-08-02

MongoDB 是由C++語言編寫的,是一個基於分散式且面向文件儲存的開源資料庫系統。

下載地址:

https://www.mongodb.com/download-center/community

在.Net Core中使用需要引入核心包 MongoDB.Driver

新增資料:

//與Mongodb建立連線
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//獲得資料庫,沒有則自動建立
IMongoDatabase db = client.GetDatabase("db1");
//拿到集合(表)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var data = new Student();
data.id = 1;
data.name = "江北";
data.age = 22;
data.remarks = "暫無";
//新增一條資料
student.InsertOne(data);

在圖形化介面中檢視一下

 Mongodb預設用id做主鍵,因此不會顯式的指定id是主鍵。Mongdb中沒有內建"自增欄位",可以把id宣告為ObjectId型別,這樣插入以後就自動給欄位賦值。

例如,建一個類:

public class School
{
    public ObjectId id { get; set; }
    public string name { get; set; }
    public string address { get; set; }
}
//需引入名稱空間 using MongoDB.Bson;

 當然School物件之後多加或者去掉一個欄位都行。Mongodb是用Json儲存的,因此也可以直接用Json格式插入,可用BsonDocument物件作為泛型物件。

//與Mongodb建立連線
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//獲得資料庫,沒有則自動建立
IMongoDatabase db = client.GetDatabase("db1");
//拿到集合(表)
IMongoCollection<BsonDocument> document = db.GetCollection<BsonDocument>("School");
db.GetCollection<BsonDocument>("School");
var json = "{id:1,name:'xx學校',address:'xxx路xx號',remarks:'暫無!'}";
BsonDocument bsons = BsonDocument.Parse(json);

 學生和學校是有對應關係的,我們可以新增有巢狀關係型別的物件

public class Student
{
    public int id { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public string remarks { get; set; }
    public School School { get; set; }
}
//與Mongodb建立連線
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//獲得資料庫,沒有則自動建立
IMongoDatabase db = client.GetDatabase("db1");
//拿到集合(表)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
Student student1 = new Student();
student1.id = 2;
student1.name = "北晚舟";
student1.age = 22;
student1.remarks = "暫無";
School school = new School();
school.name = "xxxSchool";
school.address = "xxxAddress";
student1.School = school;
student.InsertOne(student1);

 資料查詢:

//與Mongodb建立連線
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//獲得資料庫,沒有則自動建立
IMongoDatabase db = client.GetDatabase("db1");
//拿到集合(表)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var data = Builders<Student>.Filter.Gt(m => m.age, 21);//Gt:大於
var result = student.Find(data).ToList();

 我們安裝的NuGet包是支援Lamda表示式的,可用條件表示式來查詢資料

//與Mongodb建立連線
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//獲得資料庫,沒有則自動建立
IMongoDatabase db = client.GetDatabase("db1");
//拿到集合(表)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var data = Builders<Student>.Filter.Where(m => m.age > 21 && m.name.Contains("江"));
var result = student.Find(data).ToList();

分頁查詢:

//與Mongodb建立連線
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//獲得資料庫,沒有則自動建立
IMongoDatabase db = client.GetDatabase("db1");
//拿到集合(表)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var filter = Builders<Student>.Filter.Where(m => m.age > 21);
FindOptions<Student, Student> findOpt = new FindOptions<Student, Student>();
findOpt.Limit = 2;
findOpt.Skip = 1;
findOpt.Sort = Builders<Student>.Sort.Ascending(m => m.age).Descending(m => m.name);
var result = (student.FindAsync(filter, findOpt).Result).ToList();

資料更新:

//與Mongodb建立連線
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//獲得資料庫,沒有則自動建立
IMongoDatabase db = client.GetDatabase("db1");
//拿到集合(表)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var filter = Builders<Student>.Filter.Where(m => m.age > 21);
var update = Builders<Student>.Update.Set(m => m.name, "皮卡丘");
//update Student set name="皮卡丘" where age>21
student.UpdateMany(filter, update); 

 資料刪除:

//與Mongodb建立連線
MongoClient client = new MongoClient("mongodb://127.0.0.1");
//獲得資料庫,沒有則自動建立
IMongoDatabase db = client.GetDatabase("db1");
//拿到集合(表)
IMongoCollection<Student> student = db.GetCollection<Student>("Student");
var filter = Builders<Student>.Filter.Where(m => m.age > 21);
//delete from Student where age>21
//student.DeleteMany(filter);
student.DeleteOne(filter);//只刪除一個

MongoDB中文網:https://www.mongodb.org.cn

 

 

 

相關文章