您好,登錄后才能下訂單哦!
這篇文章主要介紹“.Net Core怎么使用MongoDB”,在日常操作中,相信很多人在.Net Core怎么使用MongoDB問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”.Net Core怎么使用MongoDB”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
前言
MongoDB 是由C++語言編寫的,是一個基于分布式且面向文檔存儲的開源數(shù)據(jù)庫系統(tǒng)。
下載地址:
https://www.mongodb.com/download-center/community
在.Net Core中使用需要引入核心包 MongoDB.Driver
添加數(shù)據(jù):
//與Mongodb建立連接 MongoClient client = new MongoClient("mongodb://127.0.0.1"); //獲得數(shù)據(jù)庫,沒有則自動創(chuàng)建 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 = "暫無"; //添加一條數(shù)據(jù) student.InsertOne(data);
在圖形化界面中查看一下
Mongodb默認(rèn)用id做主鍵,因此不會顯式的指定id是主鍵。Mongdb中沒有內(nèi)置"自增字段",可以把id聲明為ObjectId類型,這樣插入以后就自動給字段賦值。
例如,建一個類:
public class School { public ObjectId id { get; set; } public string name { get; set; } public string address { get; set; } }//需引入命名空間 using MongoDB.Bson;
當(dāng)然School對象之后多加或者去掉一個字段都行。Mongodb是用Json保存的,因此也可以直接用Json格式插入,可用BsonDocument對象作為泛型對象。
//與Mongodb建立連接 MongoClient client = new MongoClient("mongodb://127.0.0.1"); //獲得數(shù)據(jù)庫,沒有則自動創(chuàng)建 IMongoDatabase db = client.GetDatabase("db1"); //拿到集合(表) IMongoCollection<BsonDocument> document = db.GetCollection<BsonDocument>("School"); db.GetCollection<BsonDocument>("School"); var json = "{id:1,name:'xx學(xué)校',address:'xxx路xx號',remarks:'暫無!'}"; BsonDocument bsons = BsonDocument.Parse(json);
學(xué)生和學(xué)校是有對應(yīng)關(guān)系的,我們可以添加有嵌套關(guān)系類型的對象
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"); //獲得數(shù)據(jù)庫,沒有則自動創(chuàng)建 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);
數(shù)據(jù)查詢:
//與Mongodb建立連接 MongoClient client = new MongoClient("mongodb://127.0.0.1"); //獲得數(shù)據(jù)庫,沒有則自動創(chuàng)建 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表達(dá)式的,可用條件表達(dá)式來查找數(shù)據(jù)
//與Mongodb建立連接 MongoClient client = new MongoClient("mongodb://127.0.0.1"); //獲得數(shù)據(jù)庫,沒有則自動創(chuàng)建 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"); //獲得數(shù)據(jù)庫,沒有則自動創(chuàng)建 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();
數(shù)據(jù)更新:
//與Mongodb建立連接 MongoClient client = new MongoClient("mongodb://127.0.0.1"); //獲得數(shù)據(jù)庫,沒有則自動創(chuàng)建 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);
數(shù)據(jù)刪除:
//與Mongodb建立連接 MongoClient client = new MongoClient("mongodb://127.0.0.1"); //獲得數(shù)據(jù)庫,沒有則自動創(chuàng)建 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中文網(wǎng):https://www.mongodb.org.cn
總結(jié)
到此,關(guān)于“.Net Core怎么使用MongoDB”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。