您好,登錄后才能下訂單哦!
小編這次要給大家分享的是.Net Core如何使用MongoDB,文章內容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
前言
MongoDB 是由C++語言編寫的,是一個基于分布式且面向文檔存儲的開源數據庫系統。
在.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);//只刪除一個
看完這篇關于.Net Core如何使用MongoDB的文章,如果覺得文章內容寫得不錯的話,可以把它分享出去給更多人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。