您好,登錄后才能下訂單哦!
這篇文章主要介紹“.Net Core怎么使用MongoDB”,在日常操作中,相信很多人在.Net Core怎么使用MongoDB問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”.Net Core怎么使用MongoDB”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
前言
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
總結
到此,關于“.Net Core怎么使用MongoDB”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。